- Home /
Collision On Object Error. Please help.
Hello, so I put together a script that would destroy the object that has the script if it hits the player. However, the object does not break. The script is
pragma strict
function OnCollisionEnter (hit : Collision) {
if(hit.transform.gameObject.name == "Player") {
Destroy(gameObject);
}
}
My game is 2D. The name of the player is simply Player. I am quite new to Scripting and Unity so all help is appreciated.
You've posted two questions the same. $$anonymous$$ake sure that both objects have Collider2D on them. $$anonymous$$ake sure at least one of the objects has a rigidbody attached.
Step 1 of solving every problem ever: put in liberal Debug.Logs to see whether the function is even being called and, if so, what is the name of the object with which it is colliding.
Answer by malkere · Feb 22, 2015 at 12:25 PM
It sounds to me like you are wanting to see when the gameobject hits the player, not when the player hits the game object. though you're using the player as collider.
I've seen problems where when the player is not moving things won't trigger properly against its inanimate colliders.... ie: if I move the player a little bit then his colliders will update and the trigger would finally happen.
I'm not an expert on collision, but like tanoshimi says, add some debugs to see when it's happening and what it's happening on. very common problem.
Your answer
Follow this Question
Related Questions
Expand object on touch? 1 Answer
Transform.LocalScale Script Problem. Please help. 2 Answers
Expand Object On Touch? Someone please help me 1 Answer
Collision On Specific Object= Destroy not working. 2 Answers
Scaling Script? 1 Answer