OnTiggerEnter Issue :) [Solved]

Hey guys, I have an issue where my object is destroyed on anything set to trigger. I thought that I specified my object is only destroyed on the “DEATHCUBE” !!! (echoing voice of doom).

function OnTriggerEnter(other:Collider)
{
	if(other.gameObject.Find("DeathCube"))
	{
	Destroy (gameObject);
	}
}

Nope, your code is using GameObject.Find, which isn’t appropriate for that. I guess you want to compare the name or tag instead.

–Eric

Well, firstly, it’s best not to use the whole Find thing as its fairly slow - “Unity - Scripting API: GameObject.Find”

Also, if there are going to be many objects that are DEATHCUBES!!!, it’s best if you use the tagging system.

Finally, what you’re doing is checking to see if that command returns true (EDIT - Pretty much meaning, is there a gameObject named Deathcube, which clearly there is /EDIT), not if the “other” GameObject is named “DEATHCUBE”!!! So instead of your solution, use this…

function OnTriggerEnter(other:Collider)
{
if(other.name == "DeathCube")
{
Destroy (gameObject);
}

//the other option, using tags

function OnTriggerEnter(other:Collider)
{
if(other.tag == "DeathCube")
{
Destroy (gameObject);
}

This isn’t tested, but should work. And now mind your place, stay away from those… DEATHCUBES! Lol, goodluck. Let me know if that works.

Damn you Eric… lol. I was mid posting and you beat me with short and sweet… I should learn something from this…

enragedmrt: Thanks mate! works perfectly. I see how “find” was the wrong method to use since it looks through the whole scene. Hurray success.

No problem, glad I could help. I’m still very much a “noob” of sorts. But seeing as though I consider myself a “noob” rank 2 or so, I figure I can still try and contribute to the community that’s helped me by helping the occasional rank 1, or 2 or so. Hehe :smile:. Goodluck.

Oh, and suggestion, I find it helpful if people add the [Solved] part to the title once it’s all good. So people know they can either find an answer or help with one. Thanks again