Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get All children, children of children in Unity3d

So far i have tried this to get all children of the object but it is only brings two child object. Not all child of the child. how do i get all and loop through to get specific name object

Transform[] objChild = gameObject.transform.GetComponentsInChildren<Transform>();
for (var i = 0; i < objChild.Length; i++)
{
    objWheel.Add(objChild[i].gameObject.transform.GetChild(i).gameObject);
    objWheelAnimation.Add(objChild[i].gameObject.transform.GetChild(i).GetComponent<Animation>());
    if (objChild[i].gameObject.name.Contains("Wheel Rotation"))
    {               
    }
}
like image 366
Muhammad Faizan Khan Avatar asked Oct 20 '25 12:10

Muhammad Faizan Khan


1 Answers

If you want to get each and every child of a parent GameObject then, Here is the smallest and simple code snippet. Attach this to the parent GameObject.

        foreach (Transform g in transform.GetComponentsInChildren<Transform>())
        {
            Debug.Log(g.name);
        }
like image 121
Muhammad Faizan Khan Avatar answered Oct 22 '25 02:10

Muhammad Faizan Khan