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
0
Question by elpedro_75 · Apr 13, 2014 at 09:22 PM · gameobjectarraychildren

Creating an array of immediate children of a game object

Hello,

Inside a game object, I am trying to make an array of other game objects which are its immediate children. It is easy to find how many immediate children the game object contains using transform.childcount, but the only methods that I can think of to store them as a collection (array) are very verbose.

To clarify the problem further, my GameObject is a MenuObject, which may contain submenu MenuObjects. I'm trying to find the submenu objects of any given MenuObject. I have tried numerous things, none of which were successful. At present, I have:

             // Populate an array of submenu objects
     
             submenuObjects = new MenuObject [transform.childCount];
     
             for (int i = 0; i < transform.childCount; i ++)
             {
                 Transform next = transform.GetChild (i); // this works
                 submenuObjects [i] = next.gameObject; // can't cast between MenuObject and gameObject
             }

In this scenario, I have a reference to the GameObject of a MenuObject, but I don't know how to place that in my array. Is there a simple way to create an array of immediate children in C#?

Thank you in advance,

Peter

Comment
Add comment · Show 5
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 joshpsawyer · Apr 13, 2014 at 10:42 PM 0
Share

You could call this from the GameObject in question:

 Transform[] children = GetComponentsInChildren< Transform >();

Then you'd have an array of the childrens transforms, and you can grab the child GameObject in question with

 children[i].gameObject

Edit: Answer moved to comment. I didn't realize this was a recursive function, thanks @robertbu and @elpedro_75 for pointing that out.

avatar image elpedro_75 joshpsawyer · Apr 13, 2014 at 11:10 PM 0
Share

Thank you for your response.

I tried using GetComponentsInChildren, but I found that it returned both the object in question and the grandchildren. Hence, if an object had two children, and one of those children had a child, it would return four values.

avatar image robertbu · Apr 13, 2014 at 11:27 PM 1
Share

Assu$$anonymous$$g you are absolutely sure that all children have the '$$anonymous$$enuObject' script attached, the last line would be:

 submenuObjects [i] = next.GetComponent<$$anonymous$$enuObject>();

Note your question does not indicate the real problem. That is, you want to build an array of a specific components of immediate children.

While not as clean, you could use GetComponentsInChildren() as @joshpawyer indicated and just ignore any entries where the transform.parent is not the transform above the immediate children.

avatar image elpedro_75 · Apr 13, 2014 at 11:37 PM 0
Share

Thank you, roberbu! That is exactly what I needed. I considered the second solution, but decided it was just getting too messy.

Yes, you are quite right; I should have stated my problem more clearly too.

Thank you, both, for your help.

avatar image joshpsawyer · Apr 14, 2014 at 12:43 AM 0
Share

Whoops, I broke UA. I converted my answer to a comment, this question now has -1 answers

0 Replies

· Add your reply
  • Sort: 

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

22 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

Related Questions

Iterate through the list of child objects within a game objects 2 Answers

Find Child of GameObject from Array in Different Script 0 Answers

Use an objects (from array) position to focus a camera on 3 Answers

Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers

Detection if a GameObject is below you or next to you? 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