- Home /
 
List all children in array?
Could someone give me and example to find all the children of an object and list them in an array?
Answer by Hydr0x · May 14, 2015 at 04:58 PM
If you want to add the entire list of gameObject children to an array use this method.
Unfortunately gameobject.childCount / gameObject.GetChild() don't exist, however it does for transform. so we can use that method to find the gameObjects and add them to the array.
 private GameObject[] m_gameObjects;
         
 private void Start()
 {
     m_gameObjects = new GameObject[transform.childCount];
     for (int i = 0; i < transform.childCount; i++)
     {
         m_gameObjects[i] = transform.GetChild(i).gameObject;
     }
 }
 
               I hope this helps :)
I get an error that says get_main can only be called from the main thread?
Damn thanks. i had a ton of levels in the same scene and i was tired of referencing them individually. Now it's way a lot easier. I can focus on just creating the levels. It sure did helped mate
Answer by toddisarockstar · Aug 06, 2016 at 02:38 AM
     public GameObject thingy;
     public int count;
     public GameObject[] thingyparts;
     public void Start(){
         count=0;
         foreach(Transform i in thingy.transform){
             count++;
         }
         thingyparts = new GameObject[count];
         count=0;
         foreach(Transform i in thingy.transform){
             thingyparts[count]=i.gameObject;
             count++;
         }
 
 
 
     }
 
              Answer by StupidlySimple · Aug 06, 2016 at 01:26 PM
Not sure if it work, but probably something like this:
 using UnityEngine; // Duh.
 using System.Collections.Generic; // To Use List because List is awesome :)
 
 public class StupidlySimpleFunction {
     public static GameObject[] getChildren(GameObject parent, bool recursive = false)
     {
         List<GameObject> items = new List<GameObject>();
         for (int i = 0; i < parent.transform.childCount; i++)
         {
             items.Add(parent.transform.GetChild(i).gameObject);
             if (recursive) { // set true to go through the hiearchy.
                 items.AddRange(getChildren(parent.transform.GetChild(i).gameObject,recursive));
             }
         }
         return items.ToArray();
     }
 }
 
               You can call this function by just go StupidlySimpleFunction.getChildren(GameObject) since it's static.
Set recursive to true if you want multiple layers, like children of children of children of a GameObject etc...
Answer by rockbyte · Sep 23, 2018 at 11:18 PM
Transform implements IEnumerable, so you can cast it to a generic IEnumerableand use LINQ to handle it, which translates to this:
 Transform[] allChildTransforms = transform.Cast<Transform>().ToArray();
 
              Answer by sacredgeometry · Apr 11, 2020 at 07:57 PM
     public IEnumerable<GameObject> Children => 
         GetComponentsInChildren<Transform>()
              .Where(t => t != transform)
              .Select(t => t.gameObject);
 
               Like this?

Your answer