The object won't "die"

Hey Folks :slight_smile:

this might be a sort of odd question but why doesn’t my game object gets destroyed?

This is my code:

function OnControllerColliderHit(hit : ControllerColliderHit)
{
	if (hit.gameObject.tag == "death")
		{
		Destroy(gameObject);
		}
}

The Object A is falling on Object B wich is tagged “death” but nothing is happening … did I forget anything in the code?

Cheers !

Dawnreaver

Hmm…

Is this script attached directly to the object you want destroyed?

Something like:

GameObject A - contains Collider and Script

Not a nested Gameobject or such?

Have you put a Debug.Log() inside this trigger to assure it’s being called on collision?

I’m not sure about the vanilla Destroy(gameObject) function, I doubt it’s different to GameObject.Destroy(), but that’s the one I use.

Good luck!

-JFFM

I tried the Debug.Log he doesn’t seem to recognise the collision at all … When I try to use gameObject.Destroy() he says I have to fill in something between tghe brackets but use gameObject.Destroy(gameObject) nothing happens T_T

GameObject.Destroy is an inherited function from Object.Destroy, which is just Destroy, so there is no difference.

–Eric

Try using:

Destroy(hit.gameObject);

Hey novashot :slight_smile:

I tried your code but it didn’t work either :frowning:

Have a function on the target you want to destroy where it will kill itself like:

function Die() 
{
     // Do whatever you need to do
     Destroy(gameObject); // this should destroy the game object this script is attached to
}

then on your trigger instead of calling the destroy call use something like this:

function OnControllerColliderHit(hit : ControllerColliderHit)
{
	if (hit.gameObject.tag == "death")
		{
                    // this will send a message to the collided object to call it's die function.... if it doesn't have one the call is ignored
		     hit.collider.SendMessageUpwards("Die", SendMessageOptions.DontRequireReceiver);
		}
}