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 JBoy · Mar 02, 2013 at 05:44 AM · gameobjectarraylist

List all children in array?

Could someone give me and example to find all the children of an object and list them in an array?

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 · Mar 02, 2013 at 06:18 AM 1
Share

http://answers.unity3d.com/questions/10417/how-can-i-access-the-children-of-a-transform.html

avatar image JBoy · Mar 02, 2013 at 06:57 AM 0
Share

Thanks :) it helps alot.

5 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Hydr0x · May 14, 2015 at 04:58 PM

If you want to add the entire list of gameObject children to an array use this method.

Unfortunately gameobject.childCount / gameObject.GetChild() don't exist, however it does for transform. so we can use that method to find the gameObjects and add them to the array.

 private GameObject[] m_gameObjects;
         
 private void Start()
 {
     m_gameObjects = new GameObject[transform.childCount];
     for (int i = 0; i < transform.childCount; i++)
     {
         m_gameObjects[i] = transform.GetChild(i).gameObject;
     }
 }

I hope this helps :)

Comment
Add comment · Show 3 · 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 JamesBrodski · Aug 05, 2016 at 05:58 PM 0
Share

I get an error that says get_main can only be called from the main thread?

avatar image FinnTess · Dec 13, 2018 at 10:41 PM 0
Share

You save my life, thx !!!!

avatar image Aashishsinng · Apr 11, 2020 at 12:21 PM 0
Share

Damn thanks. i had a ton of levels in the same scene and i was tired of referencing them individually. Now it's way a lot easier. I can focus on just creating the levels. It sure did helped mate

avatar image
2

Answer by toddisarockstar · Aug 06, 2016 at 02:38 AM

     public GameObject thingy;
     public int count;
     public GameObject[] thingyparts;
     public void Start(){
         count=0;
         foreach(Transform i in thingy.transform){
             count++;
         }
         thingyparts = new GameObject[count];
         count=0;
         foreach(Transform i in thingy.transform){
             thingyparts[count]=i.gameObject;
             count++;
         }
 
 
 
     }
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 StupidlySimple · Aug 06, 2016 at 01:26 PM

@JamBroski @toddisarockstar

Not sure if it work, but probably something like this:

 using UnityEngine; // Duh.
 using System.Collections.Generic; // To Use List because List is awesome :)
 
 public class StupidlySimpleFunction {
     public static GameObject[] getChildren(GameObject parent, bool recursive = false)
     {
         List<GameObject> items = new List<GameObject>();
         for (int i = 0; i < parent.transform.childCount; i++)
         {
             items.Add(parent.transform.GetChild(i).gameObject);
             if (recursive) { // set true to go through the hiearchy.
                 items.AddRange(getChildren(parent.transform.GetChild(i).gameObject,recursive));
             }
         }
         return items.ToArray();
     }
 }

You can call this function by just go StupidlySimpleFunction.getChildren(GameObject) since it's static.

Set recursive to true if you want multiple layers, like children of children of children of a GameObject etc...

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 JamesBrodski · Aug 06, 2016 at 02:40 PM 0
Share

Awesome it works! thanks.

avatar image
1

Answer by rockbyte · Sep 23, 2018 at 11:18 PM

Transform implements IEnumerable, so you can cast it to a generic IEnumerableand use LINQ to handle it, which translates to this:

 Transform[] allChildTransforms = transform.Cast<Transform>().ToArray();


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 sacredgeometry · Apr 11, 2020 at 07:57 PM

     public IEnumerable<GameObject> Children => 
         GetComponentsInChildren<Transform>()
              .Where(t => t != transform)
              .Select(t => t.gameObject);


Like this?

alt text


screenshot-2020-04-11-at-210733.jpg (485.5 kB)
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

16 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

Related Questions

GameObject to Array/List or whatever fits best 0 Answers

How to select an Game Object from an Item List 0 Answers

How to create an array of Transforms 1 Answer

How to drag and drop any object to any empty space? 0 Answers

[C#] Sorting a List of Gameobjects Alphabetically 2 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