Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by VINNUSAURUS · Mar 15, 2018 at 12:59 PM · parentparent-childparent transformparent and child

How to get children from a Parent and not get the children of the children too

i have this code

 GameObject AC = GameObject.Find(_parent); //Find the Constutiency object
 
 Transform[] acs = AC.GetComponentsInChildren<Transform>();

i just need to get the child objects of the parent but the child objects too have children and those child objects of the child object are also grabbed.

i read this post link text

i have iterated through transform to get the children, is this the only way to do it ?

Comment
Add comment · Show 1
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 haruna9x · Mar 15, 2018 at 01:53 PM 0
Share

Yes, that's the fastest method you have.

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Remy_Unity · Mar 15, 2018 at 01:05 PM

Well, the answer was in the linked post :

 GameObject AC = GameObject.Find(_parent); //Find the Constutiency object
 
 foreach (Transform child in AC.transform)
 {
   //child is your child transform
 }
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 VINNUSAURUS · Mar 15, 2018 at 01:21 PM 0
Share

Yeah but is this the only way to achieve this ? is there a simple way to just get children ? and not the childrens children too ?

avatar image Remy_Unity VINNUSAURUS · Mar 15, 2018 at 01:43 PM 1
Share

Other way (if you want to store the children in an array), is to use the method mentioned by @donutLaserDev . Pretty similar to $$anonymous$$e.

And no, it's the only way to access direct children of an object : https://docs.unity3d.com/ScriptReference/Transform.html

avatar image VINNUSAURUS Remy_Unity · Mar 15, 2018 at 02:26 PM 0
Share

Thank you, that solved my problem.

Show more comments
avatar image
1

Answer by donutLaserDev · Mar 15, 2018 at 01:06 PM

You can use this:

 GameObject AC = GameObject.Find(_parent); //Find the Constutiency object
  
 Transform[] acs = new Transform[AC.transform.childCount];
 
 for (int i = 0; i < AC.transform.childCount; ++i) {
     acs[i] = AC.transform.GetChild (i);
 }
 
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 meat5000 · Mar 15, 2018 at 02:12 PM

Does childCount also count grandchildren? If it doesnt perhaps you can use the figure returned to iterate your list hopefully skipping all grandchildren.

I had another thought, perhaps if you just GetChild the First Child you can then use GetSiblingIndex to discover the children on the same level.

Never tried this, its just a concept.

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 donutLaserDev · Mar 15, 2018 at 02:13 PM 0
Share

childCount tells the number of immediate children only, it doesn't count the grandchildren.

avatar image meat5000 ♦ donutLaserDev · Mar 15, 2018 at 02:35 PM 0
Share

Awesome. So its simple case of using that number to truncate the list.

avatar image
0

Answer by Harinezumi · Mar 15, 2018 at 04:15 PM

If I understand it correctly, you want all the immediate child transforms of parent. In that case, this question seems to provide a solution.
If what you want is to get components only in the direct children then this forum thread seems to have the best solution (3 versions, towards the bottom). To summarize, iterate through the child transforms of parent's transform and put the components in a List.

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

78 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

Related Questions

Find a rigidbody's root parent then find its child by name 1 Answer

Problem with parent gameObject not where it should be after adding childs 0 Answers

Why my parent follows its child game object? 0 Answers

Following object (arrow) slides off of a object 1 Answer

Parent-Child axial reference problem 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