x


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.

more ▼

asked Aug 08 '12 at 10:09 AM

justinsroy gravatar image

justinsroy
18 1 1 4

Could you properly reformat the code, so that it is readable?

Aug 08 '12 at 10:34 AM Benproductions1

Can you update to your latest not working code?

Aug 08 '12 at 08:17 PM Benproductions1
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Aug 08 '12 at 10:42 AM

Benproductions1 gravatar image

Benproductions1
1.5k 5 13

I've already cleaned up his code ;)

Aug 08 '12 at 10:43 AM Bunny83
(comments are locked)
10|3000 characters needed characters left

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;
    }
}
more ▼

answered Aug 08 '12 at 10:42 AM

Bunny83 gravatar image

Bunny83
45.2k 11 49 207

Yes I forgot about telling him to put the fix joint thing after the destroy, but why the return; ?

Aug 08 '12 at 10:51 AM Benproductions1

;) Because it's useless to attach a fixedjoint when you destroyed both object right before ;)

Aug 08 '12 at 11:17 AM Bunny83

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???

Aug 08 '12 at 12:23 PM Benproductions1

Thank you for the responses and the cleaned up code, unfortunately it's still not responding when colliding with the child asset.

Aug 08 '12 at 01:52 PM justinsroy

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 terminate the execution and depending on where the next try-catch block is it could even terminate your application.

Aug 08 '12 at 02:32 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1278
x420
x409
x26

asked: Aug 08 '12 at 10:09 AM

Seen: 607 times

Last Updated: Aug 08 '12 at 08:17 PM