# Blender collision sensor is triggered twice

I have this script with the module collision which is called when the player collides with any of two cylinders in the scene, the module increments the counter in a text object that in a separate scene that overlays the main scene

import bge

scene = bge.logic.getCurrentScene()

dynamicText = scene.objects['Text']

def collision():
dynamicText.text = str(int(dynamicText.text)+1)


This the logic behind the cylinders that triggers the collision and send the message to the text object to increment the counter.

This is the login in the the text object

The problem is that the counter is incremented twice per collision, and I don’t see an obvious way to filter the second time the module is called.

This is the blend file.

If I remember correctly, collision sensors fire twice by design. Once when set True, and another when False.

This isn't a problem when connected to an And or Not controller (because they check) but in a script you'll have to do something like this:

def collision(cont):
# Loops through all connected sensors and returns if one is False
# Basically makes it work like an And conroller.
for sens in cont.sensors:
if not sens.positive:
return
...

• Also this helped me to figure out a way to test only the sensor that receives the message sent by the colliding cylinder. – rraallvv Nov 18 '14 at 23:59

I don't know why message pulses twice but

I changed the logic on Text to message> property "collideCount" add 1

Then added an always sensor true level triggering at frequency 3 connected to script

import bge
scene = bge.logic.getCurrentScene()

dynamicText = scene.objects['Text']

def main():
dynamicText.text = str(dynamicText["collideCount"])
main()


new logic http://ball2000.net/blender/images/messageBounce.png

• I see what you mean, the first time message is triggered it adds 1 to collideCount, the second time it copies the value to text, also adding 0.5 each time messageis called could work too. – rraallvv Nov 18 '14 at 3:16
• A good a theory as any; I honestly don't know why it works! : ) – ball2000 Nov 18 '14 at 9:55
• Prof Monster doesn't say anything about messages pulsing twice; and I couldn't get the double-count thing to happen by modifying the 'homework...' or 'ending' the message-sending object.. Just something weird about your blend I think... blenderartists.org/forum/showthread.php?249078 – ball2000 Nov 18 '14 at 20:55