- Home /
Destroy(transform.root.gameObject );
Ok, so I unfortunately hate to post a lot, because through trial and error I can usually get what I want working by reading other peoples help request, and also just working through 10 different ways and eventually finding it.
var speed = 100;
function Awake ()
{
renderer.material.color = Color(Random.value, Random.value, Random.value);
}
function OnCollisionEnter(collision: Collision)
{
if (collision.rigidbody)
{
var fixedJoint: FixedJoint = gameObject.AddComponent(FixedJoint);
fixedJoint.connectedBody = collision.rigidbody;
fixedJoint.breakForce = 1000;
fixedJoint.breakTorque = 1000;
if (collision.gameObject.tag == "Projectile")
{
Destroy(collision.gameObject);
Destroy(gameObject);
Destroy(transform.root.gameObject);
}
}
}
The outcome I want (Which i thought the end line of code would do) is that if i collide with a child object (a sphere in this case) I want both the child and the parent, and the projectile to get destroyed, all 3 gone. Thank you. Everything is working fine: Hit the parent object, both child/parent and projectile gone, but when I hit the child, only the child/projectile die.
Could you properly reformat the code, so that it is readable?
Answer by Bunny83 · Aug 08, 2012 at 10:42 AM
All you need is:
Destroy(collision.gameObject);
Destroy(transform.root.gameObject);
When you destroy the parent, all childs will also be destroyed.
Btw. You should be the destroy check before you link the fixed joint...
function OnCollisionEnter(collision : Collision)
{
if (collision.rigidbody)
{
if (collision.gameObject.tag == "Projectile")
{
Destroy(collision.gameObject);
Destroy(transform.root.gameObject);
return;
}
var fixedJoint : FixedJoint = gameObject.AddComponent(FixedJoint);
fixedJoint.connectedBody = collision.rigidbody;
fixedJoint.breakForce = 1000;
fixedJoint.breakTorque = 1000;
}
}
Yes I forgot about telling him to put the fix joint thing after the destroy, but why the return; ?
;) Because it's useless to attach a fixedjoint when you destroyed both object right before ;)
Yes, but that part of the code would never run, since the script got destroyed, making the return; completely useless! That's whyI don't understand why you put it there???
Thank you for the responses and the cleaned up code, unfortunately it's still not responding when colliding with the child asset.
The script will complete this function. Destroy is always delayed until the end of the current frame. Destroy can't make you exit the current function the only way is to exit it manually (with a return) or you raise an Exception which will also ter$$anonymous$$ate the execution and depending on where the next try-catch block is it could even ter$$anonymous$$ate your application.
Answer by Benproductions1 · Aug 08, 2012 at 10:42 AM
You are destroying the script, before it can destroy the parent, just get rid of the destroy child thing, because it will destroy the child anyways!!
Btw don't ever have empty function just lying around, that's terrible coding.
Benproductions1 Hope this helped
Your answer
![](https://koobas.hobune.stream/wayback/20220613075343im_/https://answers.unity.com/themes/thub/images/avi.jpg)