- Home /
Problem with collisions and Destroy(gameObject).
I'm making a Space Invaders clone for my first unity project so I can get comfortable with the engine. I'm trying to make the bullet colliding with the ship destroys the ship. Here is the code:
function OnControllerColliderHit (hit:ControllerColliderHit)
{
if (hit.gameObject.tag == "bullet")
{
Destroy(gameObject);
}
}
I have the code attached to the ship and the bullet has the "bullet" tag. My question is why isn't the ship being destroyed?
Edit:
I changed the code to:
function OnControllerColliderHit (hit:ControllerColliderHit)
{
Debug.Log("Collision Works");if (hit.gameObject.tag == "bullet")
{
Destroy(gameObject);
}
}
It did not say the collision works in the console. Thanks for all the help by the way =)
@$$anonymous$$, if your question has been answered please mark the most helpful answer as accepted
Answer by Peter G · Dec 03, 2010 at 04:20 AM
If the debug isn't being called then the function isn't running.
So, make sure you do in fact have a CharacterController on the ship. Otherwise, you should use OnCollisionEnter()
.
Make sure you are moving your bullets using Unity's physics engine. If you use Transform.Translate() to move your bullets then the message won't be sent either.
For the character controller, you should use exclusively Controller.Move()
.
Answer by Rennat · Dec 02, 2010 at 08:01 PM
to narrow down where your code is not working you can use Debug.Log(message);
to let you know what is actually happening. because with this code either OnControllerColliderHit
is not getting called or the hit.gameObject.tag
is never "bullet".
logging a message "collider hit" inside OnControllerColliderHit
would let you know that it is in fact getting called.
Sorry I'm new to this. I searched for a while on google and can't find how to work the Debug.Log. Could you tell me how that works?
just add the line Debug.Log("hi"); and it will print "hi" to the console (the window where code error notifications pop up) letting you know that line of code is getting executed, or the value of a variable with something like Debug.Log("speed: " + speed);
Your answer
Follow this Question
Related Questions
Destroying Gate by hitting a switch 1 Answer
Make an object destroy the object it is touching? 2 Answers
Destroy object if collision is strong enouf 1 Answer
Clones of object wont disapear.. 1 Answer
If two of the same objects spawn on top of each other, is it possible to destroy only one? 0 Answers