• 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
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
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.

  • ‹
  • 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

modifing enemies target inside a radius 1 Answer

Problem with GetComponentinChildren and Instantiate 0 Answers

How getting transform of another GameObject C#? 1 Answer

How to use "GetComponent" and "transform" code in dll file? 0 Answers

Endless 3D plane repetition animated by script is not moving 0 Answers


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