- Home /
need help to destroy game object
hello all i got this scrip and iv been trying all day to make it when the gameObject.tag == "player" collides with the object its destroys the gameObject please help me
private var destroy = false;
function CharacterController (hit : Collider) { if(hit.gameObject.tag == "player") { destroy = true; } } function DestroyNow() { DestroyObject (gameObject); }
If this is answered, please mark it as correct so it's not bumped back to the top of the unanswered list. If not, comment back and I'll try to help out.
Answer by · Sep 09, 2010 at 02:48 AM
You don't need the boolean or separate function. This code will immediately destroy the gameObject that collides with the player object.
function OnControllerColliderHit (hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "player")
{
Destroy(gameObject);
}
}
I find the Unity Script Reference is a great resource for checking syntax. It also usually has a helpful example for the specific code. If you search "CharacterController", you'd find the correct syntax for the OnControllerColliderHit function. Worth a try in future.
Hope this helps! If you don't understand, feel free to comment back.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How would I destroy the gameobject that a raycast hit? (SOLVED) 1 Answer
Destroying Game Object On MouseButtonDown + Distance Collision 0 Answers
Accesing Player colliderhit with other gameobject? 2 Answers
Destroying a game object. 1 Answer