- Home /
Destroy On Contact
I have a player and an enemy. Player has the tag PLAYER and ENEMY has the tag Enemy.
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "Player")
{
Destroy(collision.gameObject);
}
}
This simple code does NOT work, cause what I want to happen is the player game object to be destroyed once the enemy comes in contact with the player. I tried putting the script on the enemy character prefabs etc but nothing happens.
Same comments as always: do your objects have collides? Are they marked as "Is Trigger"? Does one of them also have a non-kinematic rigid body?
Btw if you use 2D you should use OnCollisionEnter2D and Collision2D ins$$anonymous$$d.
@tanoshimi kinematic rigidbodies also work.
@incorrect "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached."
http://docs.unity3d.com/ScriptReference/Rigidbody.OnCollisionEnter.html
Answer by 767_2 · Nov 08, 2014 at 07:59 PM
i think you got PLAYER tag and looking for Player tag (case sensitive)
Your answer
Follow this Question
Related Questions
Javascript. Respawning a character after destroyobject(). 0 Answers
destroy prefabs 1 Answer
Trouble with upgrading towers 1 Answer
Destroy Clone when colliding with 2d box collider 0 Answers
2d edge collider wont destroy? 0 Answers