- Home /
Question by
sandhillceltic · Mar 16, 2013 at 04:12 PM ·
javascriptcollisiondelete
How do I delete an object on collision with another object?
I am trying to make a game where the player comes in collision with a mesh, and another mesh is deleted. So, if the player collides with a button, the door that the button relates to, opens, without deleting the button.
This is what I have so far, that does not work. (warning) this messed up two of my prefab thirdperson script, I don't know if it will for you.
var thing : GameObject;
function OnControllerColliderHit(collision:Collision) {
if( collision.gameObject.tag == "door" ) {
Destroy(thing.gameObject);
}
}
So the var "thing" would be the door, and this script would be attached to the button.
Comment
Best Answer
Answer by ByteSheep · Mar 16, 2013 at 04:17 PM
Since you are adding the script to the button you don't want to check if it collides with the door, you want to check if it collides with the player.. Try this:
var thing : GameObject;
function OnCollisionEnter(collision:Collision) {
if( collision.gameObject.tag == "Player" )
{
Destroy(thing.gameObject);
}
}
Also don't forget to tag the player as "Player"!