- Home /
How to properly destroy object
I'm have Prefab (House), that consist from two parts - Walls and Roof. Also, script attached to House Prefab pCylinder1 (Walls) and pSphere1 (Roof)
When I throw a missile at my Home Prefab to destroy house, missile with this code:
function OnCollisionEnter ( collision : Collision) {
Destroy (collision.gameObject);
//Destroy (collision.transform.parent.gameObject);
}
Destroy (gameObject); // missile ends here
But when I hit House with my Missile (for example, I hit it in the roof), it's only pSphere1 (roof) destroyed. And script attached to my House prefab does not destroy script, it's running even after roof and walls destroyed. I used empty gameObject to nest my House, but with no result. If I destroy parent of collision object instead of object, it works fine, but it's ends up with error when i hit more simple gameObject - which contain only one element (ForExample, GameObject -> Cube) What should I do, to have a Cube like behavior on my House Object?
Answer by sparkzbarca · Dec 11, 2012 at 08:27 AM
go to the parent then
foreach (transform child in parent.transform) { destroy(child.gameobject); }
I'm using javascript, I don't get what how to do foreach (transform child in parent.transform)