• 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 c$$anonymous$$ldren 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
avatar image JBoy · Mar 02, 2013 at 06:57 AM 0
Share

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 c$$anonymous$$ldren to an array use t$$anonymous$$s method.

Unfortunately gameobject.c$$anonymous$$ldCount / gameObject.GetC$$anonymous$$ld() 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.c$$anonymous$$ldCount];
     for (int i = 0; i < transform.c$$anonymous$$ldCount; i++)
     {
         m_gameObjects[i] = transform.GetC$$anonymous$$ld(i).gameObject;
     }
 }

I hope t$$anonymous$$s 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
avatar image FinnTess · Dec 13, 2018 at 10:41 PM 0
Share
avatar image Aashishsinng · Apr 11, 2020 at 12:21 PM 0
Share
avatar image
2

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

     public GameObject t$$anonymous$$ngy;
     public int count;
     public GameObject[] t$$anonymous$$ngyparts;
     public void Start(){
         count=0;
         foreach(Transform i in t$$anonymous$$ngy.transform){
             count++;
         }
         t$$anonymous$$ngyparts = new GameObject[count];
         count=0;
         foreach(Transform i in t$$anonymous$$ngy.transform){
             t$$anonymous$$ngyparts[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 somet$$anonymous$$ng like t$$anonymous$$s:

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

You can call t$$anonymous$$s function by just go StupidlySimpleFunction.getC$$anonymous$$ldren(GameObject) since it's static.

Set recursive to true if you want multiple layers, like c$$anonymous$$ldren of c$$anonymous$$ldren of c$$anonymous$$ldren 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
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, w$$anonymous$$ch translates to t$$anonymous$$s:

 Transform[] allC$$anonymous$$ldTransforms = 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> C$$anonymous$$ldren => 
         GetComponentsInC$$anonymous$$ldren<Transform>()
              .Where(t => t != transform)
              .Select(t => t.gameObject);


Like t$$anonymous$$s?

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.

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

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

Enabled all gameobjects in array 1 Answer

Deactivate current object in array, activate next one? 2 Answers

How to put gameObjects to the list? 4 Answers

2D GameObject Array to 2D List 1 Answer

Can't add GameObjects to ArrayList 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