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
1
Question by dhawalbanker · Mar 21, 2014 at 03:58 PM · transformsceneforeachordertransforms

What is the order in which transforms are stored in .unity (scene) file?

When we loop "foreach" on any particular transform, we get the transform childs in specific order. I want to know what is that order. I could not find any doc on internet explaining the same.

What at least I figure out that, it returns back in the order in which it is stored in the unity file. Now, the question remains is in what order .unity files are stored?

Thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by vexe · Mar 23, 2014 at 05:54 PM

From what I tried,

it seems that they're sorted in the order the game objects are created - probably by their instance ID (you could see the id if you switch to debug mode in the inspector - top left corner)

In other words, they're not sorted by name. So if you create "go1", "go2" and "go3" (in that order) parent them, and print them in foreach loop, you'll get them in order, as expected. But, if you rename say, "go3" to "aaa" - it would show "go1", "go2" and "aaa"

However you could always get the sorted (by name) order of children easily:

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

     foreach (var t in ts.OrderBy(t => t.name))
         print(t.name);

Wrap it in an extension method:

 public static class Extensions
 {
     public IEnumerable<Transform> GetSortedChildren(this Transform parent)
     {
         int size = parent.childCount;
         Transform[] children = new Transform[size];
         for (int i = 0; i < size; i++) {
             children[i] = parent.GetChild(i);
         }
         return children.OrderBy(t => t.name); // don't forget using System.Linq;
     }
 }

Now...

     foreach (var t in transform.GetSortedChildren())
         print(t.name);

You could also pass a delegate to order the transforms with (so you could order by name, or anything...)

Comment
Add comment · Show 7 · 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 dhawalbanker · Mar 26, 2014 at 08:26 AM 0
Share

I loved the class extension method. But I was more looking for understanding how the transforms childs are returned.

How confident are you that the childs are list by the order in which the objects are created? Is there any formal document? Because I have seen the cases where it was not following it.

I did some digging and found that the order in which the transform's information is stored in .unity file (can be checked in any text editor), foreach returns the child object in exact same order.

Now the main question is, in what order unity file is updated? Is that anywhere documented?

avatar image vexe · Mar 26, 2014 at 08:38 AM 0
Share

I'm confident as far as my tests went - I tried creating childs, rena$$anonymous$$g them, etc. each time I print I always get the order they were created in.

what you mean with "transforms in .unity file"?

avatar image dhawalbanker · Mar 26, 2014 at 11:45 AM 0
Share

Read as, GameObject's information.

Open the .unity time in any text editor and you will see how GameObjects and chile GameObjects are represented.

avatar image dhawalbanker · Apr 28, 2014 at 01:41 PM 0
Share

This is not an expectable answer to me. I was asking something else. $$anonymous$$oderator forcefully gave the green signal.

Not done!!

avatar image vexe · Apr 28, 2014 at 08:09 PM 0
Share

@dhawalbanker: relax, someone must have been on an answer cleaning tour, came across this one thought it was answered but not ticked. I unticked. You should frequently visit your unanswered questions if you care about them - keep pinging till you get a satisfying answer.

Show more comments

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

How to transform a object to another when a object of tag "Hitter" hit it? 1 Answer

Prefab Transform & Light Child Transform 1 Answer

How to make a character move on Local axis? 1 Answer

How do I replicate Unity's movement/rotation/scaling handles? 1 Answer

how to get game object by string? 0 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