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 /
avatar image
1
Question by pretender · Nov 29, 2010 at 12:14 PM · hierarchychildren

getting the children of the first level in hierarchy

if i have this hierarchy:

root
     object a
      subobject a-1
      subgroup a-2
        subobject a-2-1
        subobject a-2-2
      subobject a-3
     object b
      subobject b-1
      subobject b-2
     object c
      subobject c-1
      subobject c-2

how to get the first level children with the parent being, for example, object a? if i use Array(objecta.GetComponentsInChildre(Transform)) then i get not only first level which is subobject a-1,subgroup a-2 and subobject a-3 but also children of the subgroup a-2

is there a way to get the first level children of given parent? thanks, any help appreciated!

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

4 Replies

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

Answer by Mike 3 · Nov 29, 2010 at 12:35 PM

Something like this:

Array array = new Array();

for (var child : Transform in objecta.transform) { array.Add(child); }

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
2

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 · Show 1 · 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 TooManySugar · Oct 25, 2018 at 08:17 PM 0
Share

this is vine, I'll be adding this to my utils class.

I was thinking of doing similar way like get all childrens and check if the parent transform was the given one, it it was then it was the first levle child , similar to yours. I asume childcount will only count first level children tho, otherwise won´t work.

avatar image
0

Answer by frogsbo · Jun 11, 2014 at 03:29 AM

first level children:

     var g = Selection.activeGameObject;
     for (var child : Transform in g.transform) 

all children:

 var g = Selection.activeGameObject;
 var allChildren = g.GetComponentsInChildren(Transform);
 for (var child : Transform in allChildren) {}
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
0

Answer by drudiverse · Jul 20, 2014 at 12:38 PM

The transforms are returned in order of the top level downwards if you get all objects in the parent, so if you know the number of children that should be in the first level. i.e. 16, you can just cycle through objects 1-17 of the array... object 0 with be the parent.

so using that method, it's possible to cound the first level childs using the first option, and then read from all transforms using that number + 1 to skip the parent.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Hierarchy Foldout Variables Wanted 3 Answers

transform.Find always returns null 1 Answer

Change an object's grandparent 2 Answers

Is there a way to make clones appear as children in the hierarchy? 3 Answers

I can't move gameobjects or create parents/children in the hierarchy menu, what's going wrong? 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