• 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
4
Question by bigbat · Jul 17, 2013 at 09:20 PM · transformgetcomponentdepthgetcomponentinchildren

How to get first depth child components of a parent object?

Hi to all.

If i have an gameobject that has more than one child gameobjects and each of it's child also have their child.Is it possible or is any way to get transform component of all first level child ? like below diagram where each of child repeat it's parent structure and i want access transform of child 1 until child n:

gameobject

child 1

child 2

.

.

child n

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 robertbu · Jul 17, 2013 at 09:42 PM 0
Share

Note the bit of source at the top of the file at this link:

http://docs.unity3d.com/Documentation/ScriptReference/Transform.html

avatar image bigbat · Jul 18, 2013 at 12:10 AM 0
Share

Thanks for reply

I already saw that link,but my question is how to get transform of child 1,child 2 ,..and child n ,and not transform of their child or all child transform of current gameobject(since child 1 ,..,child n themselves have child objects).

7 Replies

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

Answer by s_guy · Jul 18, 2013 at 12:19 AM

A brute force way would be to iterate all of a transform's children. For each child, check to see if the the child's transform.parent is the same as the transform you started with. If it is, then it's a first level child.

You could set up a loop or recursive check for each child until the above match is made and count each recurrence to see how far you had to go.

Comment
Add comment · Show 2 · 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 bigbat · Jul 18, 2013 at 08:28 AM 0
Share

Yes i had already thought of that,but i.e. in a complex model with very much child objects(In my current case it really is complex object,a player model with many child)whether it does not affect performance?

avatar image bigbat · Jul 18, 2013 at 10:10 AM 0
Share

I figure out my problem with a different solution.indeed i'm working on an inventory system like that in "risen" game,and this question was about equiping and unequiping an item to a player body part.

But since i think there isn't no better answer for my primary question,so i mark this answer as correct,because it works.

avatar image
21

Answer by sathya_m · Oct 09, 2013 at 09:02 AM

in C#

If you are looking for only first depth of Gameobject childs

foreach(Transform trans in object.transform)

     {
              Debug.Log("child"+ trans.name);
     }


//////////////////////////////////////

if you want to read child form all depth of hierarchy

Transform[] transforms = Object.GetComponentsInChildren();

     foreach(Transform trans in transforms)
     {
              Debug.Log("child"+ trans.name);
     }

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 Miscellaneous · Nov 26, 2013 at 03:23 AM 0
Share

This is the best solution, thanks.

avatar image huulong · Sep 16, 2014 at 02:07 PM 0
Share

Thanks, I was wondering whether I was getting deeper children by mistake with the 1st formula but actually it was not the case. I guess object.GetComponentsInChildren also returns the component T of object, so you would also get the transform of the said object with the second formula.

avatar image tylerwongj · Jun 25, 2019 at 01:05 AM 0
Share

Why does foreach(Transform trans in gameObject.transform) {} result in getting the first-depth?

avatar image Nameelee · Mar 31 at 09:16 AM 0
Share

Wow. That's brilliant. Thank you.

avatar image
5

Answer by vexe · May 05, 2015 at 05:18 AM

In case someone wants to get depth values in a gameObject hierarchy:

 void LogDepth(Transform root, int depth)
 {
     var children = root.GetComponentsInChildren<Transform>();
     foreach(var c in children)
     {
         if (c.parent == root)
         {
             print(c + ": " + (depth + 1));
             LogDepth(c, depth + 1);
         }
     }
 }

eg: consider the following hierarchy:

 root
 -- A
 ---- B
 ------ C1
 ------ C2

root is at depth 0, A is at 1, B is at 2 while C1 and C2 at depth 3

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
3

Answer by Kerihobo · Jan 24, 2017 at 09:25 AM

Sorry, I know this is old, but can you simply say

 foreach (Transform t in someTransform) {
     if (t.parent == someTransform) {
         Debug.Log(t);
     }
 }
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 V-Jankaitis · Oct 11, 2017 at 01:55 PM

 public static Transform[] GetTopLevelChildren(Transform Parent)
 {
     Transform[] Children = new Transform[Parent.childCount];

     for (int ID = 0; ID < Parent.childCount; ID++)
     {
         Children[ID] = Parent.GetChild(ID);
     }

     return Children;
 }
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
  • ›

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

Accessing the Transform of a child GameObject of my Parent GameObject? 1 Answer

Get Components without parent? 2 Answers

Having trouble understanding parenting with componants 0 Answers

GetComponent for a stored transform. 0 Answers

Getting mesh renderer from a Transform(not "transform") 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