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. Dismiss Notice

Question How to loop through parent and also all the children ?

Discussion in 'Scripting' started by shamenraze1988, Feb 25, 2021.

  1. shamenraze1988

    shamenraze1988

    Joined:
    Nov 24, 2020
    Posts:
    208
    Code (csharp):
    1.  
    2. void Start()
    3.     {
    4.         foreach (Transform g in transform.GetComponentsInChildren<Transform>())
    5.         {
    6.             Animator a = g.GetComponent<Animator>();
    7.             if(a != null)
    8.             {
    9.                 a.enabled = false;
    10.             }
    11.         }
    12.     }
    13.  
    I need to loop through all the children of children all levels but also the top parent root.

    In the loop I need to include also the Anim_Flyer_Transportation top object and not only the children.

     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Why not just use GetComponentsInChildren<Animator>()?
     
  3. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    this is what PraetorBlue mean
    Code (CSharp):
    1. //better
    2.         private void TurnAllAnimtorsOff(Transform root)
    3.         {
    4.             Animator[] animators = root.GetComponentsInChildren<Animator>();
    5.  
    6.             foreach (Animator a in animators)
    7.             {
    8.                 a.enabled = false;
    9.             }
    10.         }

    to answer your question how to loop all childs transform
    Code (CSharp):
    1. //This is how I loop all the transform childs, dont use this if you only try to get all components from all child
    2.         private void LoopThroughAllChildsTransform(Transform root)
    3.         {
    4.             foreach (Transform g in root.GetComponentsInChildren<Transform>())
    5.             {
    6.                 Animator a = g.GetComponent<Animator>();
    7.                 if (a != null)
    8.                 {
    9.                     a.enabled = false;
    10.                 }
    11.  
    12.                 LoopThroughAllChildsTransform(g);
    13.             }
    14.         }
     
    yale3d and shamenraze1988 like this.
  4. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    Try something like this. I wrote it as an extension method so you can reused for other work.
    I haven't compiled, wrote in notepad at work, so may be some spelling errors to work out.

    Code (CSharp):
    1. public static class Extentions
    2. {
    3.     public static List<GameObject> GetChildrenRecursive(this GameObject _this, bool returnParent = true)
    4.     {
    5.         List<GameObject> returns = new List<GameObject>();
    6.         if (returnParent == true){
    7.             returns.add(_this);
    8.         }
    9.      
    10.         foreach(Transform t in _this.transform){
    11.             returns.AddRange(t.gameObject.GetChildrenRecursive(true));
    12.         }
    13.     }
    14. }
    Store this as a new script file. And on your 'Anim_Flyer_Transportation' object call GetChildrenRecursive(true) on your object.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Well what PraetorBlue said is already the answer to the question. Your last code snippet is not needed as GetComponentsInChildren already does search for deeply nested children. It returns components on the given object and any nested children. Your code would iterate over nested children several times. Your code would make sense if you used something like what Cyber-Dog used, even though his implementation is horrible inefficient ^^. Such recursive implementations should take a List as parameter and not create a new list for every element.
     
  6. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    i dont do GetComponentsInChildren I just keep the way the poster was write and I told he/she not to use it
    this is the original I loop through childcount
    Code (CSharp):
    1. public static void LoopThroughAllChildsTransform(Transform root)
    2.         {
    3.             for(int i =0; i < root.childCount; i++)
    4.             {
    5.                 Transform t = root.GetChild(i);
    6.  
    7.                 Debug.Log(t.gameObject.name);
    8.  
    9.                 LoopThroughAllChildsTransform(t);
    10.             }
    11.         }
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    I'm a bit confused by your post. The code you just posted doesn't look anything like the original code and also not like anything you posted before. Yes, the code you just posted is a correct recursive approach. Though as I said GetComponentsInChildren does this already for you.
     
  8. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    the point is not about using API like GetComponentsInChildren, I show The poster the c# technique how to do auto loop by having method of itself inside for alternative, the mistake I make is over look Poster's code and I end up go along with it , you had point it out my mistake so I corrected my code of what it means to do.