Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
3
Question by unifiedMan · Jun 28, 2012 at 08:52 PM · transformchildorderiterate

Iterating through children of transform: is there an order guarantee?

If I iterate through the children of a transform with a foreach loop, is there any guarantee for the order in which the children are enumerated?

E.g. if I set up a test scene with the following hierarchy:

(Cube with four children, named A, B, C, D

And then I use a foreach loop to iterate through the children of "Cube":

     foreach(Transform child in this.transform)
         Debug.Log(child.name);

What happens is that A B C D is always printed in alphabetical order. Is this a guarantee though? An alphabetical-order traversal isn't defined in the documentation for Transform.

The reason why I ask is because I am iterating over multiple transform hierarchies that have an identical structure, and it would be nice to know if there is some sort of order guarantee when iterating through children.

testHierarchyScreenshot.png (4.3 kB)
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
7
Best Answer

Answer by whydoidoit · Jun 28, 2012 at 08:53 PM

I don't think there is a guarantee and if there is an order it would more likely to be the order that you added them.

You could guarantee your own order like this:

   using System.Linq;
   using System.Collections.Generic;


   ...


   foreach(var child in transform.Cast<Transform>().OrderBy(t=>t.name))
   {
   }
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 Eric5h5 · Jun 28, 2012 at 09:05 PM 2
Share

There is indeed no guaranteed order. If there's none mentioned, you can assume that there isn't any. (In fact there's no guaranteed order for anything in Unity that involves arrays.)

avatar image MaxLohMusic · Mar 22 at 06:57 AM 1
Share

Most people would want and expect the order they added them. I found the answer in another post:

 foreach (var child in parentTransform.Cast<Transform>().OrderBy(t => t.GetSiblingIndex()))
avatar image
1

Answer by MaxLohMusic · Mar 22 at 06:58 AM

You can set the order by order you added them thus

 foreach (var child in parentTransform.Cast<Transform>().OrderBy(t => t.GetSiblingIndex()))
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Child Transform 1 Answer

Make a GameObject child to a "Dragged Reference" GameObject doesn't work! 0 Answers

Changing child's rotation changes parent's position 0 Answers

Properly Rotating Child Objects by Script 1 Answer

How can I access the children of a Transform? 5 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