Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
30
Question by msknapp · Jan 13, 2012 at 08:22 PM · parentchildchildrenparent-child

How To Get List of Child Game Objects

Sometimes a game object has child game objects. Likewise, a game object can have a parent. In the past I tried getting all child game objects by using this:

 List gs = new List();
  Transform[] ts = gameObject.GetComponentsInChildren<Transform>();
  if (ts == null)
  return gs;
  foreach (Transform t in ts) {
  if (t != null && t.gameobject != null)
  gs.Add(t.gameobject);
  }
  return gs;



But I have recently proven this is unreliable when running from the editor. I have loaded in from a collada file the main game object. In the hierarchy view, I see the game object with all of its children. However, my script returns an empty list.

Can somebody please tell me a reliable way to get child objects?

Can somebody please tell me a reliable way to get the parent game object?

Comment
Add comment · Show 2
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 msknapp · Jan 13, 2012 at 09:00 PM 0
Share

Basically what I did was AssetDatabase.Load$$anonymous$$ainAssetAtPath, and followed it up with GameObject.Instantiate. With the instance it creates, I see in the hierarchy view there are children game objects. Still, the script returns an empty list.

avatar image ma69kra · Mar 15, 2021 at 01:11 PM 0
Share

A clever recursive function to obtain all the children of a game object down multiple levels of hierarchy! https://github.com/aniketrajnish/get-all-children-of-a-gameobject/blob/main/ChildList.cs

14 Replies

· Add your reply
  • Sort: 
avatar image
92
Best Answer

Answer by Julien-Lynge · Jan 13, 2012 at 10:36 PM

The suggestions I've seen from smart people like Eric5h5 is to use the build-in transform functions, rather than GameObject.Find. Since it looks like you're using C#, the following is also in C#:

To get children:

foreach (Transform child in transform)
{
  //child is your child transform
}

To get parent:

transform.parent;
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 msknapp · Jan 13, 2012 at 10:41 PM 16
Share

why on earth cannot unity just build this capability into their API? ridicolous.

avatar image DCrosby · May 04, 2015 at 06:34 PM 2
Share

The issue as stated before is that this returns the Transform of "transform" in the foreach loop, adding: if (child != transform[0]){} will eli$$anonymous$$ate the transform you're wanting to get children from.

which would change the code to:

 foreach (Transform child in transform)
 {
     if (child != transform[0]){
        //child is your child transform
     }
 }

avatar image ntszar · Aug 17, 2015 at 08:38 PM 0
Share

@DCrosby I just checked and that does not seem to be the case. Iteration over the transform does not include itself.

avatar image isteffy · Mar 09, 2016 at 06:39 PM -1
Share

I don't like this solution since it finds the child's transform and not the child itself. $$anonymous$$aybe I'm missing something...

avatar image Pinkuboxu isteffy · May 02, 2020 at 10:14 PM 0
Share

Just in case you didn't figure it out after 4 years, honestly not busting your chops at all, transform.gameObject will be that transforms actual game object. Generally when working in Unity, the transform contains most of the same properties and points to the same Components as they both inherit the Object class.

avatar image GameDevSA · Aug 02, 2017 at 02:21 AM -1
Share

$$anonymous$$y solution was similar but here is the full code, and you can get to the game object itself from it:

 Transform[] allChildren = GetComponentsInChildren<Transform>();
          foreach (Transform child in allChildren)
          {
              child.gameObject.SetActive(false);
          }

So as you can see it's easy to get to the gameObject.

avatar image Bunny83 GameDevSA · Aug 02, 2017 at 03:32 AM 0
Share

You just spam the same comment on all questions that have anything to do with finding child objects. This comment makes no sense as you posted exactly the same code the OP used in his question.

Please stop bumping three years old, answered question for no reason.

avatar image
13

Answer by pprofitt · Jun 07, 2014 at 02:40 PM

         public List<GameObject> Children;


         foreach (Transform child in transform)
         {
             if (child.tag == "Tag")
             {
                 Children.Add(child.gameObject);
             }
         }

In c# you can use this code to get all the child objects with a certain tag.

Comment
Add comment · Show 4 · 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 dval · Oct 19, 2015 at 07:13 PM 0
Share

I use this method. Even if your not filtering by tag, storing the children in a List is the only way to use methods like: System.Array.Reverse(Children) because Transform doesn't have a built-in GetEnumerator.

avatar image nadavrt · Oct 08, 2019 at 01:46 AM 1
Share

Your first line of code is incorrect. When declaring the Children list there is no need to add public in front of it. Children is a local variable, not a property. It will be available to the foreach loop anyway, because they are both on the same function scope. Also, you are not instantiating the list, which some IDEs (such as Visual Studio) will report as an error. In short, you should fix the first line to: List<GameObject> Children = new List<GameObject>();

avatar image AGameByBrad nadavrt · May 29, 2020 at 07:58 AM 0
Share

Should Children not also be lowercase to follow convention?

avatar image sacredgeometry AGameByBrad · Feb 06, 2021 at 04:21 PM 0
Share

Follow unitys but not follow nearly all of c# conventions. Including that of the .NET framework.

So no it is wrong and Unity should feel bad about it. :)

avatar image
4

Answer by Pix10 · Jan 19, 2013 at 07:00 PM

foreach(Transform child in transform) is unreliable, as is GetComponentsInChildren, as they won't traverse the entire tree of children.

This will correctly return all the children:

     void Start () {
      
         Debug.Log("Child Objects: " + CountChildren(transform));
     }
 
     int CountChildren(Transform a)
     {
         int childCount = 0;
         foreach (Transform b in a)
         {
             Debug.Log("Child: "+b);
             childCount ++;
             childCount += CountChildren(b);
         }
         return childCount;
     }
Comment
Add comment · Show 3 · 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 ErvinDjinn · Jan 29, 2013 at 09:16 PM 3
Share

No it won't, you're script returns the number of children not a list of them. Besides you can just use transform.childCount to get the number of children anyway.

avatar image ebogguess · Feb 28, 2013 at 06:49 PM 0
Share

As far as I know, "children" refers to the transforms on the next level only, while "descendants" would refer to every transform below this one in the tree. If the goal was to iterate over all descendants, the code example shows a way to do that recursively.

avatar image gaps · Apr 10, 2020 at 04:00 PM 0
Share

This is generically wrong, GetComponentsInChildren does transverse the entire tree of children, not just the immediate children. It might not behave correctly when used with Transform's though.

avatar image
1

Answer by jason891107 · Oct 26, 2012 at 04:02 AM

foreach (Transform child in transform) { //child is your child transform } This is right.

GetComponentsInChildren will get gameObject's transform as well.

Comment
Add comment · 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
1

Answer by Dinkelborg · Jan 13, 2014 at 10:50 PM

The best Answer would be a recursive function I guess ...

So the function will go and look at the first Transform it finds in transform then it will do whatever you want to do (add it to a list or array or whatever) in this case it will change the active and passive colour stored in a level element... after it has done what it should it will see if the object has any children.

"!" Although this works sometimes there seems to be a real problem with the childCount function in about 50 percent of all objects it does not see the last layer of the hierarchy.

"!!" There is also one problem with this: If you add new layers OnStart of the Game and you also execute the script at the same time as these will get created it might not see them... -?- Is there any way to time this or should I just use a yield WaitForSeconds in the start function...?


 public void Start () 
     {
         StartCoroutine(OverrideAllChildren(transform));
     }
 
     public IEnumerator OverrideAllChildren(Transform pTransform)
     {
         foreach(Transform child in pTransform)
         {
             TouchQube temp = child.GetComponent<TouchQube>();
             if(temp != null)//If it is a TouchQube
             {
                 totalCount++;
                 if(overrideInactive){temp.SetInactiveLight(overrideColor);}
                 if(overrideActive){temp.SetActiveLight(overrideColor2);}
             }
             Debug.Log(child.name+" has "+CountChildren(child)+" children. TotalOverwritten: "+totalCount);
             if(HasChildren(child))//If it has children
             {
                 StartCoroutine(OverrideAllChildren(child));
             }
         }
         yield return(true);
     }
 
     public bool HasChildren(Transform pTransform)
     {
         int count = 0;
         foreach(Transform child in pTransform)
         {
             count++;
         }
         if(count > 0){return true;}else{return false;}
     }
 
     public int CountChildren(Transform pTransform)
     {
         int count = 0;
         foreach(Transform child in pTransform)
         {
             count++;
         }
         return count;
     }



Comment
Add comment · 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
  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 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 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

Change an object's grandparent 2 Answers

Make a simple tree 1 Answer

Get Children of Child and Deactivate/Activate on Demand 1 Answer

Does transform.root work when a parent is made the child of another object through script? 1 Answer

Getting the topmost parent 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