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 /
avatar image
0
Question by NanoEngine · Mar 15, 2017 at 04:20 PM · transformarraychildrenreturn type

Return array of transform

Hello, I'm currently coding a function which will return a array of children. But I can't figure out how to return an array of children. This is the code I have so far:

     //Find multiple children
     Transform[] FindChildrens(Transform parent)
     {
         foreach (Transform child in parent.transform)
         {
             return child;
         }
         return null;
     }
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

2 Replies

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

Answer by ifdef_ben · Mar 15, 2017 at 05:11 PM

The thing wrong with your approach is that you are returning a single element, not an array.

As @Bunny83 pointed out in the comments, you could get the transform's children array by simply calling:

 parent.Cast<Transform>().ToArray(); 

But doing so would generate a lot of garbage. If you need an efficient way to get the children array you could get it by using the GetChild() method in a for loop.

  Transform[] FindChildrens(Transform parent)
  {
      //Declaring an array the size of the number of children
      Transform[] children = new Transform[parent.childCount];
      for (int i = 0; i < children.Length; i++)
      {
          //populating the array
          children[i] = parent.GetChild(i);
      }
      //returning the array. If there is no children it will be an empty array instead of null.
      return children;
  }
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 Bunny83 · Mar 16, 2017 at 01:56 PM 1
Share

While your answer does answer the question and is providing a working solution, your second point is actually not true ^^. The Transform class implements the IEnumerable interface which let you iterate over the immediate children of the transform object.

The method could even implemented in one line:

 using System.Linq;
 // [ ... ]
 Transform[] FindChildrens(Transform parent)
 {
     return parent.Cast<Transform>().ToArray();
 }

Though the IEnumerable ultimatively also just uses childCount and GetChild. However the IEnumerator object that is required will allocate additional garbage. Also ToArray would use some sort of dynamic "List" to be able to collect all items and turn them into an array. So there's even more garbage to collect.

So performance-wise your solution is the better one.

ps: If you want to get "null" as return if there are no children, just add this at the very beginning of the "FindChildrens" method:

 if (parent.childCount == 0)
     return null;
avatar image ifdef_ben Bunny83 · Mar 16, 2017 at 04:14 PM 0
Share

That is really good to know. I was not aware of this, thank you! I corrected my answer.

avatar image
0

Answer by Matt1000 · Mar 16, 2017 at 10:46 AM

This is probably the way to go:

 GameObject parentObject;
 Transform[] childernTransforms = new Transform[parentObject.transform.childCount];
 
 for (int i = 0; i < parentObject.transform.childCount; i++) {
     childernTransforms [i] = parentObject.transform.GetChild (i);
 }

And thats all. Hope it helps :)

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

83 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Why do we access children through an object's transform? 2 Answers

How to sync child objects of a transform in multiplayer 3 Answers

move enemy to random transform array 2 Answers

Instantiate from array into array? 2 Answers

get nearest game object 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