- Home /
Why does hierarchy order change at runtime?
Hi,
I've recently begun working on a Unity project that was started by someone else.
I noticed that some top-level GameObjects have a different order in the hierarchy when the project is running in the Editor and when it's not. I don't understand why.
I can't find any call to transform.SetSiblingIndex in the project's code. What other mechanics could explain this ?
Answer by Kyle_WG · Sep 29, 2015 at 12:34 PM
Any instantiated objects may cause the hierarchy to change, especially if unparented.
The hierarchy order shouldn't be an issue anyway. If you're relying on the order consider using a container object instead and parent objects you need to that. Then you can get access to the children from that transform. Or even hold the instantiated object's references in an array or generic container instead. Relying on Unity's hierarchy order is bad practice and should be avoided.
Thanks for your reply @$$anonymous$$yle_WG. Actually, I do not use the order of the objects, it would just be more practical for me that these objects do not move when i want to inspect them at runtime, since I have to remember two positions in the hierarchy ins$$anonymous$$d of one.
Any instantiated objects may cause the hierarchy to change, especially if unparented.
Do you mean that instantiating/parenting/unparenting can affect any part of the hierarchy, even in a branch that is not concerned by those operations ?
The hierarchy is sorted using some internal methods. Check this link on BaseSorting if it helps you. It'll sort itself when the Hierarchy becomes dirty through parenting / instantiating. If an object becomes unparented for example it'll have to change sort order on the top level objects (the main parents).