Run one same script in two different object at same time

Hi, I want to make when two same type of object collide together, it will destroy both object and spawn one object.

For example, when two Cube collide, both Cube will be destroy, then a BigCube will spawn at one of Cube position.

For now my code is like

OnCollisionEnter(Collider other){

  if(other.gameObject.name == gameObject.name){
      Destroy(other.gameObject);
      Instantiate(BigCube, transform.position, transform.rotation);
      Destroy(gameObject);
 }

}

But when I run the game, if two cube collide, it will instantiate two BigCube. What should I do to prevent two BigCube instantiate to the game?

Hi @ncchieh, Sorry, I miss read your statement. You need to keep a global reference to BigCube, or a counter, or some variable you can use to test if BigCube has already been created. Some variable that both scripts have access to. So test that variable and if you have created BigCube, don’t create it again.