Trigger single collision event

For my game, I have multiple game objects for all of those I attached single collision detection script.

When object collides with other object two OnCollisionEnter method executed because each object contains same script.

I want single collision method execution per collision so what changes I have to do in my scripting?

Basically my goal is to execute single time my code withing OnCollisionEnter method. Although this question seems to be easy but frustrating me from long time.

You can write your code in a method in seperate class and call that method from your “OnCollisionEnter()”. In the other class (having that method) take boolean variable and set it false when intially and set true when once that method is executed and make sure that your code is inside the if block having condition !boolVariable.

public void YourMethod()
{
 if(!boolVariable)
 {
  //your code.
  boolVariable = true;
 }
}

I hope this would help you…

Two options really. 1) create a base class in one script with the common code, but leave out the OnCollisionEnter method. Then assign that to the object you don’t want getting the event. Then derive a new class from this that simply implements the OnCollisionEnter() method. Option 2) add a tag to the object that should handle the collision, such as Collidable, and then in the OnCollisionEnter, test for tag == “Collidable”