Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Loop through all children?

Discussion in 'Editor & General Support' started by dogzerx2, Jul 5, 2010.

  1. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,960
    I'm having difficulties finding a way to loop through all the the children of an object.

    I've got a character with a whole bone hierarchy, I want to loop through all these bones recursively.
     
    ferRod5 likes this.
  2. Arkdurath

    Arkdurath

    Joined:
    Jan 10, 2010
    Posts:
    64
    I think its something like this:
    Code (csharp):
    1.  
    2. for(int i = 0; i < this.gameobject.transform.GetChildCount(); i++)
    3. {
    4.    GameObject Go = this.gameobject.transform.GetChild(i);
    5. }
    6.  
    Hope it helps you

    LLORENS

    Edit: If you have problems about recursivity, try to make a .GetComponentsInCHildren<Transform>(), and then, foreach transform, get his gameobject.
     
    sean244 likes this.
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can iterate through an object's children with a loop like this:-
    Code (csharp):
    1. for (var child : Transform in transform) {
    2.     child.position = Vector3.zero;
    3. }
    If you want to visit the bone hierarchy recursively, you might find it easiest to use a recursive function call:-
    Code (csharp):
    1. function TraverseHierarchy(root: Transform) {
    2.   for (var child : Transform in transform) {
    3.     // Do something with child, then recurse.
    4.     TraverseHierarchy(child);
    5.   }
    6. }
     
  4. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Just incase someone stumbles across this old thread the recursive function above has a small error which will cause Unity to crash

    the second line of code should be :

    Code (csharp):
    1. for (var child : Transform in root) {
     
  5. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    I know this is old but it was listed near the top of the google results. Here's my solution of extending the Transform class.

    Code (CSharp):
    1. namespace UnityEngine.TransformExtensions {
    2.     public static class ExtensionMethods {
    3.  
    4.         /// <summary>
    5.         /// Performs a deep search for the provided name.
    6.         /// </summary>
    7.         /// <param name="trm">The transform to search through</param>
    8.         /// <param name="name">The name of the transform object to search for.</param>
    9.         /// <returns></returns>
    10.         public static Transform FindRecursive(this Transform trm, string name) {
    11.  
    12.             Transform child = null;
    13.  
    14.             // Loop through top level
    15.             foreach (Transform t in trm) {
    16.                 if (t.name == name) {
    17.                     child = t;
    18.                     return child;
    19.                 } else if (t.childCount > 0) {
    20.                     child = t.FindRecursive(name);
    21.                     if (child) {
    22.                         return child;
    23.                     }
    24.                 }
    25.             }
    26.  
    27.             return child;
    28.         }
    29.     }
    30. }
    My only concern really is performance, but depending on the size of your hierarchy and the size of the object you're searching through, this is likely much faster than just a blanket GameObject.Find call, which searches through everything.

    EDIT: I changed the name in my project (and here) from FindChildRecursive to FindRecursive to follow the naming pattern of Unity, considering FindChild is deprecated. Also, I REALLY don't recommend using this if your transform has a lot of objects. It comes in handy with dynamically built transforms that are small. Anything outside of that and you're better off storing the reference to the object.
     
    Last edited: Jan 22, 2019
    Acissathar likes this.
  6. saadali211

    saadali211

    Joined:
    Feb 17, 2017
    Posts:
    19
    looping through all the childs and nested childs in a gameobject.
    see the image for OutPut

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public  class ChildFinder : MonoBehaviour
    6. {
    7.   public List<Transform> childs  = new List<Transform>();
    8.    
    9.     void Start()
    10.     {
    11.         FindEveryChild(gameObject.transform);
    12.         for (int i = 0; i < childs.Count; i++)
    13.         {
    14.             FindEveryChild(childs[i]);
    15.         }
    16.     }
    17.  
    18.     public void FindEveryChild(Transform parent)
    19.     {
    20.         int count = parent.childCount;
    21.         for (int i = 0; i < count; i++)
    22.         {
    23.             childs.Add(parent.GetChild(i));
    24.         }
    25.     }
    26. }
    27.  
     

    Attached Files:

    arturmandas and TayyabMehboob like this.
  7. sergiobd

    sergiobd

    Joined:
    May 14, 2014
    Posts:
    36
    How about this?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SwapMaterials : MonoBehaviour
    6. {
    7.     public GameObject queryParent;
    8.  
    9.     void Start()
    10.     {
    11.         List<Transform> childs = new List<Transform>();
    12.  
    13.         Utilities.GetAllChildren(queryParent.transform, ref childs);
    14.  
    15.         foreach (Transform t in childs) {
    16.  
    17.             Debug.Log(t.name);
    18.  
    19.         }
    20.        
    21.     }
    22.  
    23. }
    24.  
    25.  
    26. static class Utilities
    27. {
    28.  
    29.     public static void GetAllChildren(Transform parent, ref List <Transform> transforms)
    30.     {
    31.        
    32.         foreach (Transform t in parent) {
    33.  
    34.             transforms.Add(t);
    35.  
    36.             GetAllChildren(t, ref transforms);
    37.  
    38.         }
    39.  
    40.     }
    41.  
    42. }
    43.  
     
  8. ademord

    ademord

    Joined:
    Mar 22, 2021
    Posts:
    49
    sorry for the late reply, how can I store a reference to that object? i have a lot of children and this loop is a huge impact on my performance.
     
  9. MartinH8921

    MartinH8921

    Joined:
    Aug 27, 2021
    Posts:
    8
    In 2022, I tried this code and it is no longer valid. I made it work with this:
    for (int i = 0; i < transform.childCount; i++)
    {
    Transform tform = transform.GetChild(i);
    }