Detect objects name you just hit

Hi all,

I have a script which when you run your character into a cube, it destroys it…and it works fine. But now i only want to destroy a cube if it has a certain name…

#pragma strict




function Start () {
}

function Update () {

}
function OnTriggerEnter ( collision : Collision )
{
if (this.gameObject.name == "Cube2"){
	GameObject.Destroy ( gameObject ) ;
	}
}

That is what I have tried…and it does not work, could anyone point me in the correct direction please? Thanks!

Well, first of all, you never need to use this. in unity. Its redundant.

Second, add a debug line to see the name of the object you hit, and you’ll be able to see what you did wrong :slight_smile:

function OnTriggerEnter ( collision : Collision )
{
    Debug.Log("You just hit " + gameObject.name);    

    if (gameObject.name == "Cube2")
    {
        GameObject.Destroy ( gameObject ) ;
    }
}

‘this’ is the object the script is attached to. Likely you want to use the ‘collision’ object rather than ‘this’. Check the script reference for Collision.