[SOLVED] Find all childs with rigidbody component ?

Hi.

I’m trying to find all childs with rigibody component and add it to array.
I don’t know how to find them ?

    public GameObject [] Ragdoll;
    public GameObject Hip;

    void Start()
    {
       
        Ragdoll = new GameObject [Hip.transform.childCount]; // It counts only first 3 objects !!?

        for (int p = 0; p < Hip.transform.childCount; p++)
        {
            print("p "+ p);
        //    Ragdoll[p] = Hip.transform.GetChild(p).gameObject;

        }


    }

You can get all of the components first:

List<Rigidbody> bodies = GetComponentsInChildren<Rigidbody>();
Ragdoll = new GameObject[bodies.Count];
for(int i = 0; i < bodies.Count; i++)
   Ragdoll[i] = bodies[i].gameObject;

It gives me this error with first line

Opps, I forgot that it returned an array instead of a collection. Change the list to an array, and use .Length instead of .Count.

1 Like

Good. Thank you so much WallaceT.