Multiple triggers with object intersect is not working

I am writing a trigger to change the state of the rectangle to state "red" when it intersect with the other object.

I wish to change the state to "normal" when the objects intersect again to each other.

I have added two triggers as mentioned below

1. Change the state of rectangle1 to "red" when the shape intersects with rectangle 2 if rectangle1's state is equal to "Normal" 

2. Change the state of rectangle1 to "Normal" when the shape intersects with rectangle 2 if rectangle1's state is equal to "red" 

Out of these 2 triggers only first one is getting executed.

Attached story file for the reference. Can anyone tell me what is wrong here.

 

2 Replies
Alyssa Gomez

Hello there! Thanks for including your file. I felt a bit like I was playing Tetris 😄.

I actually didn't see either rectangle change to red, so I'm thinking that I'm not testing your file correctly. Please take a look at this screen recording, and let me know if there's something I'm missing. Thanks!

Walt Hamilton

You need a third, impartial observer. All the triggers are executed each time there is an intersection. So When it intersects and Rectangle1 is Normal, it changes to Red, the since it is Red, it turns to Normal. For it to work, you can't keep comparing it to itself, you must compare it to another object.

I prefer to use a variable. When it intersects, change the variable, then set the state to Red if the variable is one value, and Normal if it is another value.

If you use a T/F variable, you can change it with one trigger.  It does not work if there are two triggers that change it to one state or another, depending on what state it already is. Either the variable or the object must have only one trigger. It may help you to understand if you switch the order of your triggers. You'll see that the action still doesn't work, but now the other state works.