- Home /
Destorying an object when it hits the ground
Hi, I'm trying to get my projectile "Shell" to be destroyed both when it hits the opponent, and if it misses and hits the floor. As it is now, it does print out but doesn't get destroyed. I'm sure I overlooked something small or put something in the wrong place. Any help would be appreciated!
function OnCollisionEnter(collision : Collision)
{
print( "floor");
if (collision.gameObject.tag == "Shell")
{
Destroy(gameObject);
}
}
I should also add that it does get destroyed upon hitting and dealing damage with the opponent, just not the floor.
Answer by Joshua · May 25, 2011 at 10:52 PM
First of all, I'd add this script to the shell, not the floor. Let each shell check 'am I hitting the floor' and not the floor 'am I being hit by a shell'. I'm not sure, but I could imagine that if two shells enter collision with it during the same frame, only one will get destroyed. It would also seem that in the script you posted you're destroying the floor, not the Shell. Or you're checking the shell, not the floor - it's hard to know what if I don't know if the script is attached to the floor or the shell.
This is how I would do it. Attach this script to the shell and tag the floor "Floor".
function OnCollisionEnter(col : Collision) {
if (col.gameObject.tag == "Floor") {
Destroy(gameObject);
}
}
Tried this, and the shells still aren't being destroyed. The shells are rigid bodies, not sure if that matters though.
Thanks for the help by the way!
Well this code surely works. Do you have the floor tagged Floor (check capitalization!) do the shells have a collider? Does the floor have a collider?
Shells have colliders, floor has a collider, floor is tagged. The tag is in lowercase, but I changed the code to match. I can try making them all Capitalized though.
No that doesn't matter, at long as the tag you're checking and the tag is has are the same. Is this script attached to the bullets, not the floor? o.O
Oh man...lol. Got it. I have individual pieces making up an area of the floor and grouped into "hill", "dip", "straight" etc. I tagged them as a whole without realizing that each individual piece needed the tag.
It does work now. Thaks a ton man!
Your answer
Follow this Question
Related Questions
Trouble with raycast hitting a tagged object 0 Answers
Collision Detection Issue with SetActive(false) not working 2 Answers
collision wont work 1 Answer
Create hinge joint 2D by collision 2 Answers
Collision Detection for a Prefab? 3 Answers