add rigidbody through scripting?

Hi guys,

I have a parent object, like a ball, with another object as a child inside. I have it scripted such that when the parent ball collides, it unparents the child. No problem there. The problem is that I don’t want to child object to react to physics or gravity until it’s unparented. After it’s unparented, I want it to fall, bounce etc. I’ve tried setting isKinematic to true until the parent is detached, but there’s still mass in the child object and it goes flying on it’s own away from the parent. Is there a way to make the rigidbody null until the child is unparented, then add it back? And is this the best solution? Here’s the script I’m using now that isn’t working very well:

function Update () {
if (transform.parent == "ball")
{
transform.rigidbody.isKinematic = true;
}
else
{
transform.rigidbody.isKinematic = false;
}
}

I’ve attached this to the child object.

As always, thanks in advance for your help. :roll:

Use AddComponent(Rigidbody).

–Eric

Thanks, that worked perfectly. My only other question is how can I change the rigidbody parameters once it’s added? Do I write a line of code for each: Mass, Drag etc.?

Thanks again, Eric5h5.

var rb : Rigidbody = AddComponent(Rigidbody);
rb.mass = .5;
rb.drag = 2.0;
//etc.

–Eric

Perfect. Thanks!

AddComponent(Rigidbody) is removed from unity because unity throws an error :

AddComponent is not a member of ‘myscriptname’

This thread is from 2010.

The syntax is now:

gameObject.AddComponent(Rigidbody)

I think. I only use C#.