Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

How only fire OnCollisionEnter only once for two GameObjects?

Discussion in 'Scripting' started by Kinkku2, Mar 24, 2016.

  1. Kinkku2


    Sep 29, 2014
    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


    Nov 2, 2012
    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.