- Home /
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.
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...)
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?
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"?
Read as, GameObject's information.
Open the .unity time in any text editor and you will see how GameObjects and chile GameObjects are represented.
This is not an expectable answer to me. I was asking something else. $$anonymous$$oderator forcefully gave the green signal.
Not done!!
@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.
Your answer
Follow this Question
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