Iterating child game objects in C#

I have a game object with a few child game objects like so;

GameObj1 // Parent
-GameObj2 // Child 1
-GameObj3 // Child 2
-GameObj4 // Child 3

I want to iterate through the children. From the docs it looks like one can iterate through the transforms of the children but i want to access the game objects.

Can anyone show me how to do this in C# ?

Thanks

1 Like

If you have a reference to a Transform or any other kind of Component, you can use its .gameObject property to get at the GameObject it’s attached to:

foreach(Transform child in transform)
{
	Something(child.gameObject);
}
16 Likes

Why not to use the power of C# delegates? :sunglasses:

Using the folowing class…

using UnityEngine;

namespace DankoKozar.Unity.Utils
{
    public class GameObjectUtil
    {
        public delegate void ChildHandler(GameObject child);

        /// <summary>
        /// Iterates all children of a game object
        /// </summary>
        /// <param name="gameObject">A root game object</param>
        /// <param name="childHandler">A function to execute on each child</param>
        /// <param name="recursive">Do it on children? (in depth)</param>
        public static void IterateChildren(GameObject gameObject, ChildHandler childHandler, bool recursive)
        {
            DoIterate(gameObject, childHandler, recursive);
        }

        /// <summary>
        /// NOTE: Recursive!!!
        /// </summary>
        /// <param name="gameObject">Game object to iterate</param>
        /// <param name="childHandler">A handler function on node</param>
        /// <param name="recursive">Do it on children?</param>
        private static void DoIterate(GameObject gameObject, ChildHandler childHandler, bool recursive)
        {
            foreach (Transform child in gameObject.transform)
            {
                childHandler(child.gameObject);
                if (recursive)
                    DoIterate(child.gameObject, childHandler, true);
            }
        }
    }
}

… you could do things like:

GameObjectUtil.IterateChildren(myRootGameObject, delegate(GameObject go) { go.layer = 10; }, true);

… which is handy for executing something on all children (and the grandchildren, grandgrandchildren and so on - depending on the “recursive” parameter).

6 Likes

Actually you can do

using System.Linq;

      foreach (Transform item in Container.transform)
      {
          //Do stuff
      }

I wouldn’t. Why use a more complicated approach than you need to? A loop is pretty simple and gets the job done perfectly fine.

27 Likes

Perfect solution :sunglasses:

No it’s not perfect since there’s something wayyyyy easier. Why doing int i = 10 - 10 when you can int i = 0 ?

3 Likes

Because its cooler :sunglasses:

1 Like

any way of doing it with a for loop?

As in not a foreach?

You can loop over transform.childCount and use transform.GetChild to get children at the indices.

for (int i=0; i< transform.childCount; i++)

Then you can use transform.GetChild(i).gameObject to access the child object.

4 Likes

Thank you a bit tricky deleting child gameobjects but managed in the end

Live saver!