Search Unity

How only fire OnCollisionEnter only once for two GameObjects?

Discussion in 'Scripting' started by matias-e, Mar 24, 2016.

  1. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hey! So, I have a bit of a problem: I have two GameObjects with the same script and I have a method I'd like to run from inside their OnCollisionEnter event. When these GameObjects collide, the event fires twice, which is bad. How could I make the method within OnCollisionEnter fire only once while these two GameObjects collide? Thanks.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    have you tried adding a bool? check the bool is true before doing anything, in the collision code set the bool to false for both this and the collided object. Whichever gets called first will disable the code in whichever is called second. If it's the same script on each of the gameobjects it should work i think.