Destroy 2 spawn 1

I'm having a problem with Instantiating a certain prefab.

When two objects collide, I need for them to destroy each other and spawn a single prefab.

The problem is this: When they crash, they each spawn a prefab, rather than the two spawning one prefab.

I need a solution so that one will spawn a prefab and one will just disappear. Since the collisions will be random, setting them up ahead of time using booleans is out of the question since they might simply vanish without spawning anything or spawn two instead of one.

1 Answer

1

Since it's always two objects colliding (and from what you're saying they all have the same/similar scripts attached) how about

static var collision counter : int

Every time one of your colliding objects calls OnCollisionEnter you add one. When it's 2 you instantiate the prefab and set back to zero.

Isn't that going to be a problem when 4 objects collide and you need 2 ?

I doubt four objects would collide at exactly the same moment. As soon as two collide both are destroyed and one is instantiated, so I think is should work.