- Home /
Question by
spacelyjoe · Feb 13, 2014 at 02:28 AM ·
gameobjecttriggerdestroyontriggerenterdestroy object
How come when the player runs into the coin it won't destroy?
void OnTriggerEnter(Collider theTrigger){
if (theTrigger.gameObject.name == "Player"){
PlayerHitScript.coin_count += 1;
Destroy(this.gameObject);
}
}
The code works by a coin drops after you kill a monster, and if you run into the coin it's supose to be destroyed. The code works if you run into the coin within like a second of killing the monster but if you kill it, wait a couple seconds, then run into the coin it just stays there and doesn't disappear, anyone know why?
Comment
is the coin object marked as trigger? and is this script attached to the coin object?
And does either the player or the coin have a rigid body attached ?
Answer by darklink10 · Feb 13, 2014 at 03:59 AM
The way you have this scripted is rather inefficient, on top of that you're using Unity library terms that don't exist. Try something like this
void OnTriggerEnter(Collider)
{
if(Collider.tag == "Player")
{
transform.Destroy();
}
}
In this you'll have to set the physical player's tag to "Player"