- Home /
Find children and set them all to be kinematic rigidbodies
OK, before we start, I know you shouldn't really have rigidbodies on children, but I'll try my best to explain why I do.
I'm making a game where you can move objects around to progress. One of these objects is a pencil pot, and it's full of pencils, rulers and the like. When you pick up the pencil pot, I want it to bring all the stationary inside it along with it. However, I also wish for these objects to be able to be picked up out of the pot and moved around independently, which is why they have their own rigidbodies.
When I pick up an object in my code, it sets the object to kinematic and turns collisions off on the object. Because of this, when I pick up the pot, all the pens and whatnot fall through because they still have gravity applied to them and the object they were sitting in is no longer solid. I've tried the obvious method of setting the pencils to be kinematic by default, but unfortunately my game doesnt seem to like that, as my pencil pot goes crazy and vibrates its way across the desk that it's on.
Because of all this, I want to find a way to access all of the children of the pot when I pick it up, and set them to all be kinematic too. I can't figure out how i'd do this. There doesn't seem to be simple setting to change all children, so I'm assuming I need a for loop to cycle through the children and set each one in turn, but unfortunately I can't figure out how to set up the array I'd need to do so.
If theres a more elegant/obvious way around this, I'd love to know! All help is appreciated, thanks!
Answer by Owen-Reynolds · Apr 03, 2011 at 04:22 PM
In http://answers.unity3d.com/questions/22767/how-to-see-if-transform-has-child the poster has copied the "see all kids" code from the Unity scripting docs. The answer has some more examples of doing stuff to your kids (some I haven't seen in the docs!)