- Home /
Destroy a GameObject
Hello,
I creating a projectile motion where I wish to destroy the Projected Object on touching the floor. The floor does not have a rigidbody. I think the best possible way is to use
Destroy (cannonball);
But could you please guide me as of how to destroy it.
GameObjects :
Floor and CannonBall Prefab.
Perception:
I think I would be attaching the script to the floor and checking it with any contact with the ball.
THANK YOU very much
Answer by FLASHDENMARK · Dec 05, 2011 at 06:40 PM
You want to destroy the "cannonball" when it hits the floor?
just put a script measuring collision on the cannonball prefab and destroy it on impact. Something like this:
function OnCollisionEnter (){
Destroy(gameObject); //Destroys the object the script is attached to.
}
Perfect. I got it. But I have a turret from which it fires. So I added something like this -
var floor : GameObject;
function Awake ()
{
floor = GameObject.FindWithTag ("floor");
}
function OnCollisionEnter ()
{
if(floor)
Destroy(gameObject); //Destroys the object the script is attached to.
}
Is it right?
That all depends how you want to make it work. You are checking if there is an existing gameObject called "floor" and destroys if that object exists .
What I think you mean is:
function OnCollisionEnter(hit : Collision){
if(hit.gameObject.tag == "floor"){//Checks if floor is hit
Destroy(gameObject); //Destroys this gameObject if true.
}
}
What I am doing is checking whether or not the object is hitting the floor and if it is I destroy it.
Yeah got it. Thank you very much. I appreciate your help.
Answer by bula · Dec 05, 2011 at 10:49 PM
If you want some particle effects like an explosion copy OrangeLightning code
function OnCollisionEnter (){
Destroy(gameObject); //Destroys the object the script is attached to.
}
And add some particle effect to it go game object-create other-Particle effect. Create the particle effect and make a script to activate it. If you dont know how to do it just search for the unity 3d students you'l find it there Cheers
wow thank you very much. By the way - what is orange lightning code? Where do I find it?
OrangeLightning is the guy who posted the other answer. :)
Your answer
Follow this Question
Related Questions
Destroying a prefab on collision with a cube? 1 Answer
Trouble with raycast hitting a tagged object 0 Answers
Game Over GUI Question 2 Answers
Make an object destroy the object it is touching? 2 Answers
Destroy and Spawn an Enemy 1 Answer