Whose "OnCollisionEnter" gets called first?

I think here’s a bug in the code because the result depends on whose “OnCollisionEnter” gets called first:

If the Player’s OnCollisionEnter gets called first, it detects that the obstacle’s tag is not “Hit”, and thus increase its score. The obstacle’s OnCollisionEnter gets called next and changes its tag to “Hit”.

OTOH, if the obstacle’s OnCollisionEnter gets called first, it changes its own tag to “Hit”, then the Player’s OnCollisionEnter gets called, and it detects that the obstacle’s tag is “Hit”, thus it doesn’t increase the score.

Is this correct?

Hi,

Welcome to our community! :slight_smile:

I think you are right. You could easily fix that problem by renaming one of the “last” OnCollisionEnter method to OnCollisionExit. The *Enter methods get executed before the *Exit methods.

Alternatively, you could add your concerning scripts to this list and define the execution order.

Did this help?


See also:

1 Like

Hi Nina, tanks for the quick reply. It’s good to know about this.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms