Double collisions

Hi all,

I have an issue where sometimes a collision is registered twice even though the object is destroyed.
In my collision function I do:

Destroy(gameObject);

But still sometimes the object registers a collision twice; I tried active = false and Physics.ignore collision but neither method worked. For now I fudge a bool IAmHit = true and make my collision function return if this is set. Seems like a nice bodge though!
Also tried destroy immediate which just caused Unity to crash. Is this a known issue/bug with Unity?

thanks in advance

are you using OnCollisionExit in your script? Reason I ask, is OnCollisionExit doesn’t activate when an object is destroyed, even though you think it would. Before you destroy( gameObject ), I would suggest that you move your object-to-be-destroyed a large arbitrary amount into non game space, wait for a small amount of time, then destroy the object and see if you stil have the same error.

So, what I usually do, is instead of calling destroy(), I will call a co-routine that calls destroy inside:

void DestroyFix()
{
     transform.Translate( Vector3.up * 5000 );
     StartCoroutine( "DestroySelf" );
}

IEnumerator DestroySelf()
{
    yield return new WaitForSeconds( 0.1f );
    Destroy( gameObject );
}

I’ve just encountered this in my game and it turned out that there are two colliders on one of the objects involved. (Wouldn’t it be nice if colliders could be on their own layers instead of always on the same layer as the gameObject?) Later I might just combine the two into a single mesh collider, but for now the workaround is to add a boolean named alreadyDead to the object behavior and then set it the first time the collision occurs. If it’s set on a subsequent collision then the collision is ignored.

if (!alreadyDead) {
	alreadyDead = true;
	Destroy(gameObject);
}