Search Unity

Iterating child game objects in C#

Discussion in 'Scripting' started by iphonefreak, May 12, 2009.

  1. iphonefreak

    iphonefreak

    Joined:
    Feb 25, 2009
    Posts:
    157
    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
     
    raquel08 likes this.
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    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:

    Code (csharp):
    1. foreach(Transform child in transform)
    2. {
    3.     Something(child.gameObject);
    4. }
     
  3. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Why not to use the power of C# delegates? :cool:

    Using the folowing class...

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. namespace DankoKozar.Unity.Utils
    4. {
    5.     public class GameObjectUtil
    6.     {
    7.         public delegate void ChildHandler(GameObject child);
    8.  
    9.         /// <summary>
    10.         /// Iterates all children of a game object
    11.         /// </summary>
    12.         /// <param name="gameObject">A root game object</param>
    13.         /// <param name="childHandler">A function to execute on each child</param>
    14.         /// <param name="recursive">Do it on children? (in depth)</param>
    15.         public static void IterateChildren(GameObject gameObject, ChildHandler childHandler, bool recursive)
    16.         {
    17.             DoIterate(gameObject, childHandler, recursive);
    18.         }
    19.  
    20.         /// <summary>
    21.         /// NOTE: Recursive!!!
    22.         /// </summary>
    23.         /// <param name="gameObject">Game object to iterate</param>
    24.         /// <param name="childHandler">A handler function on node</param>
    25.         /// <param name="recursive">Do it on children?</param>
    26.         private static void DoIterate(GameObject gameObject, ChildHandler childHandler, bool recursive)
    27.         {
    28.             foreach (Transform child in gameObject.transform)
    29.             {
    30.                 childHandler(child.gameObject);
    31.                 if (recursive)
    32.                     DoIterate(child.gameObject, childHandler, true);
    33.             }
    34.         }
    35.     }
    36. }
    37.  
    ... you could do things like:

    Code (csharp):
    1. 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).
     
  4. Chorinators

    Chorinators

    Joined:
    May 8, 2013
    Posts:
    1
    Actually you can do
    Code (csharp):
    1.  
    2. using System.Linq;
    3.  
    4.       foreach (Transform item in Container.transform)
    5.       {
    6.           //Do stuff
    7.       }
    8.  
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,619
    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.
     
  6. Sami_AlEsh

    Sami_AlEsh

    Joined:
    Sep 17, 2018
    Posts:
    5
    Perfect solution :cool:
     
  7. gadoy-

    gadoy-

    Joined:
    Jul 7, 2019
    Posts:
    3
    No it's not perfect since there's something wayyyyy easier. Why doing int i = 10 - 10 when you can int i = 0 ?
     
    RafaelKuhn, isaacwolfsite and tbriz like this.
  8. superbobmanjoe

    superbobmanjoe

    Joined:
    Jul 6, 2020
    Posts:
    1
    Because its cooler :cool:
     
    hosasy likes this.
  9. Pnvanol

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    115
    any way of doing it with a for loop?
     
  10. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    As in not a foreach?

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

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,627
    for (int i=0; i< transform.childCount; i++) 


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

    Pnvanol

    Joined:
    Jan 11, 2016
    Posts:
    115
    Thank you a bit tricky deleting child gameobjects but managed in the end
     
    Last edited: Oct 7, 2020
  13. joanpescador

    joanpescador

    Joined:
    Dec 21, 2016
    Posts:
    122
    Live saver!