Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 31, 2012 at 07:11 AM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
7
Question by liszto · Jul 06, 2012 at 09:46 AM · gameobjectchildrenrecursively

Recursively get all children

hi, there is a method to get all children of a game object recursively ? Cause I want sirect children but children of children too !?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jul 06, 2012 at 10:08 AM 0
Share

What language?

avatar image sonapax · Oct 11, 2013 at 01:33 PM 1
Share

// work fine for me (geted ALL obj in ierarchy), recurs

// recursively get childrens =======================================================================

 private List<GameObject>childsOfGameobject = new List<GameObject>();

 private List<GameObject> GetAllChilds(GameObject transformForSearch)
 {
     List<GameObject> getedChilds = new List<GameObject>();

     foreach (Transform trans in transformForSearch.transform)
     {
         //Debug.Log (trans.name);
         GetAllChilds ( trans.gameObject );
         childsOfGameobject.Add ( trans.gameObject );            
     }        
     return getedChilds;
 }

 // END recursively get childrens ====================================================================

// sample

void getClothObjs() { GameObject cloth = GameObject.Find ( "girlForGUZoneCLOTH" ); childsOfGameobject.Clear (); GetAllChilds (cloth);

     // debug;
     
     foreach (GameObject list in childsOfGameobject)
     {
         Debug.Log (list.name);
     }
 }
avatar image saadali211 · Jul 16, 2019 at 12:58 PM 0
Share

Here is the code to iterate through all the childs and nested childs of a gameObject.

 public List childs  = new List();
 void Start()
 {
     FindEveryChild(gameObject.transform);
     for (int i = 0; i < childs.Count; i++)
     {
         FindEveryChild(childs[i]);
     }
 }

 public void FindEveryChild(Transform parent)
 {
     int count = parent.childCount;
     for (int i = 0; i < count; i++)
     {
         childs.Add(parent.GetChild(i));
     }  
 }
avatar image Sponxie · Dec 02, 2019 at 11:20 AM 0
Share

Like this? It calls setlayer recursively.

 void Start(){
      SetLayer(transform.root, 1);
  }
  public static void SetLayer (Transform trans, int layer){
      //Set the layer Of the parent
      trans.gameObject.layer = layer;
      // Call set layer on any children
      for (int i = 0; i < trans.childCount; i++) {
          SetLayer(trans.GetChild(i), layer);
      }
  }
 

1 Reply

  • Sort: 
avatar image
19
Best Answer

Answer by tmdchi · Jul 06, 2012 at 10:34 AM

gameObject.GetComponentsInChildren(Transform);

That will return an array of all the transforms under your gameObject.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jul 06, 2012 at 10:36 AM 5
Share

Yes or

  gameObject.GetComponentsInChildren<Transform>(); in C#

Please tell us what language you are using when asking a question so it is possible to give you the most relevant answer.

avatar image Bunny83 · Jul 06, 2012 at 10:39 AM 1
Share

And all components on this gameobject as well. $$anonymous$$eep that in $$anonymous$$d ;)

You can of course get them recursively (or iteratively) by using a recursive function or a List, but since Untiy provides GetComponentsInChildren that's the easiest way.

avatar image frogsbo · Jun 07, 2014 at 02:37 PM 2
Share

example gets all children js:

 var go = Selection.activeGameObject;    
             var allChildren = go.GetComponentsInChildren(Transform);
             for (var child : Transform in allChildren) {
             print(child.gameObject.name);
                 if(child.gameObject.GetComponent($$anonymous$$eshRenderer)){
 
                     child.gameObject.AddComponent($$anonymous$$eshCollider);        
 
                 }    
             }
avatar image shefPhysics frogsbo · Apr 25, 2018 at 02:43 PM 0
Share

I like this answer, how would it be in C#? I'm getting some problems at translating it.

avatar image brando_slc shefPhysics · May 23, 2018 at 07:19 AM 0
Share

Assu$$anonymous$$g GameObject go;:

  Transform[] allChildren = go.GetComponentsInChildren<Transform>();
             foreach (Transform child in allChildren)
             {
                 Debug.Log(child.gameObject.name);
                 if (child.gameObject.GetComponent<$$anonymous$$eshRenderer>() != null)
                 {
                     child.gameObject.AddComponent<$$anonymous$$eshCollider>();
                 }
             }
 

avatar image MaDDoX · Feb 17, 2015 at 06:18 AM 1
Share

Although tmdchi's answer is wrong for the OP question, please notice that in some situations - noticeably while doing DestroyImmediate editor commands - you might get problems with the standard GetComponentsInChildren method. In these cases a slight variation of what he wrote is the easiest/safest route:

     while (gO.childCount > 0){
         foreach (Transform child in gO) {
             DestroyImmediate(child.gameObject);
         }
     }
avatar image MorphVGX · Jun 14, 2015 at 02:24 AM 0
Share
     foreach(Transform t in target.GetComponentsInChildren<Transform>(true)) //include inactive
     {        
     }

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding ChildObject 2 Answers

How to check parent value instead name value(more details in post) 3 Answers

Destroy Child Object(s) OnValidate? 6 Answers

Is it possible to FindWithTag only within children of a certain gameObject? 1 Answer

Destroy created children of objects 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges