Search code examples
unity-game-enginegameobject

Simplest way to iterate over all GameObject childrens from a script at runtime


In my Unity3D project I got a complex GameObject of a truck that his hierarchy looks like this.

+ Truck
   +FrontPivotPoint
      +LeftWheel
        Tire
        Rim
        Hindge
      +RightWheel
        Tire
        Rim
        Hindge
    +CenterPivotPoint
      +Body
        Arm
        Screw
        Pin  

Basically what's happening is I got a lot of complex parenting and I want to go trough every child and add to him a RigidBody. I think it should be something nested but I don't have something in mind. Any help would be appreciated!


Solution

  • Unity3d allows you to easily automate every routine. You may consider to add custom menu item.

    public class MakeRigidBodies : EditorWindow
    {
      [MenuItem ("Make rigid bodies %&r")]
      private static void Execute()
      {
        var selectedObject = UnityEditor.Selection.activeObject;
        if( selectedObject && selectedObject is GameObject )
        {
           // for all children of selectedObject
           {
             // add rigid body
           }
        }
      }
    }