- Home /
How unity arrange an Object's child ?
i have an Object with some child named 1-9, i try to use a "for" loop to get it's child, i'm able to get "1" as 1st child, "2" as 2nd and so, but after i rename it in hierarchy. the arrangement get mess up. I get "6" as 1st child, and then "7,8,9,1,2,3,4,5".
i wonder how unity actually arrange it ? and how do i suppose to rearrange it ?
You'll need to sort them by name, as well. I can tell you how to do that in C#, but nobody ever wants that...
i mean, except that sorting them in script by getting their name. there is no way i can change how they arrange in unity by default? as by default they will arrange according which have been create 1st.
just feel that it is so unfriendly that once i have any changes i'll need to write another line of code for the arrangement.
Well, there's no way to predict what order they will be in in the transform tree. The objects get added in some opaque way, and it almost never matters.
Answer by funfair · Feb 10, 2012 at 07:59 AM
it's actually not the matter of my code, im just wondering about this, and i believe they are putting in a array list right after they are create or put in to a parent, if you rename it when it already inside it's parent, the arrangement will still remain the same, so i don't think it's arrange by name, although it is when the moment you put it inside a parent
From my experience (limited) it is by order created in the hierarchy. While it is by when they are added if created and added at run time.
Answer by Paulius-Liekis · Jan 31, 2012 at 01:00 PM
You shouldn't not rely on order of children. Unity doesn't give any guarantees about that order.
Actually there are some guarantees and there are times that you need to rely on order... example NGUI uses the order of children to display tables and grids.
Answer by Kryptos · Jan 31, 2012 at 01:19 PM
At runtime (as well as in the hierarchy view), child objects are sorted by name. This is a side effect of the Transform.Find() method that needs a heuristic (alphabetic-order) to be efficient. I'm using this property in some of my scripts, so I'm prety sure it's true.
Note: On the other side, in the project view, child are sorted by InstanceID. This doesn't matter but it is quite confusing.
Edit: show us your piece of code with the for loop, maybe there is something wrong here.
Answer by Matfrog2000 · Feb 07, 2014 at 04:36 PM
To solve this problem, you just have to drag all the childs out of their parent and drag them back inside the parent in the order you want it to be. After that they all will be in the correct order (the order you put them in).
Answer by AShim-3D · Jun 05, 2014 at 12:33 PM
Util than allow you to sort children by name https://gist.github.com/AShim3D/d76e2026c5655b3b34e2