• 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
0
Question by michaelfelleisen · Dec 15, 2020 at 11:37 AM · transformchildrenchild object

Get components in children and childrens children and so on

I was having some trouble getting a specific component from all transforms beneath a parent object. transform.GetComponentsInChildren() only returns components of the first layer of children and not childrens children and so on.

So I wrote a small extension method. I saw a lot of posts about this so I will post my version as answer here.

Comment
Add comment
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 Reply

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

Answer by michaelfelleisen · Dec 15, 2020 at 11:43 AM


Edit: Just use GetComponentsInChildren()

 public static class Extension
     {
         public static List<T> GetComponentsInChildrenRecursively<T>(this Transform _transform, List<T> _componentList)
         {
             foreach (Transform t in _transform)
             {
                 T[] components = t.GetComponents<T>();
     
                 foreach (T component in components)
                 {
                     if (component != null)
                     {
                         _componentList.Add(component);
                     }
                 }
     
                 GetComponentsInChildrenRecursively<T>(t, _componentList);
             }
     
             return _componentList;
         }
     }

To use this do something like this:

 public Transform parent;
     
     public void Example() {
         List<Transform> transformList = new List<Transform>();
         parent.GetComponentsInChildrenRecursively<Transform>(transformList);
         //use transformList as you wish
     }

Cheers, Michael

Comment
Add comment · Show 5 · 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 Hellium · Dec 15, 2020 at 12:01 PM 0
Share

The documentation explicitely says:

Unity searches for components recursively on child GameObjects. This means that it also includes all the child GameObjects of the target GameObject, and all subsequent child GameObjects.

So I don't see the difference between your function and Unity's one.

If you have issues finding components on inactive gameObjects, GetComponentInChildren has a second boolean argument to include them.

avatar image michaelfelleisen Hellium · Jan 11 at 07:54 AM 0
Share

You are right. I have no idea why GetComponentsInChildren did not work for my case before. Now it works completely fine.

avatar image Bunny83 michaelfelleisen · Jan 11 at 08:04 AM 0
Share

Well, another common gotcha is that GetComponentInChildren will also include components of the given gameobject itself, not just on child objects. So it's doing the same as GetComponents but also includes components on all children. So there is a usecase for your custom implementation. However in most cases GetComponentsInChildren does work just fine.


ps: There's also a GetComponentsInParent method which also starts searching on the actual gameobject and includes all components in any parent up the hierarchy.

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

How Should I Get a List of Child Objects 2 Answers

Retrieve All Child Objects 1 Answer

How to move a gameObject without affecting its childrens' positions? 2 Answers

Android game lags when using transform.RotateAround to rotate an object with many children 2 Answers

ChildCount number returns incorrect 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges