- Home /
Destroy game object when player enters a box collider
Hello, Sorry this question is really simple - I want to make a game object disappear when my player enters a trigger. This is the code I've written, but I can't work out what's wrong, as although there are no errors - the object doesn't disappear?
var pylon : GameObject;
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player")
Destroy (pylon);
}
Thanks very much, Laurien
Your code seems to be correct. did you forget to assign pylon
in the inspector? or maybe change your player's tag to "Player"? there is also a common mistake when you create a tag with lower case "player" and using upper case version in code.
Answer by Eugenius · May 09, 2013 at 12:12 AM
Well, obviously you didn't attach the script to the pylon in your code. You could do that (but then lose the script when destroying) or you could attach the script to the Player and edit it as follows: var pylon : GameObject;
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "replace_here_with_pylon_tag")
Destroy (pylon);
}
Also be sure to have colliders on both the trigger and the player and a rigidbody on the player.
Hope this helps!
Your answer
![](https://koobas.hobune.stream/wayback/20220613101841im_/https://answers.unity.com/themes/thub/images/avi.jpg)