- Home /
Objects in hearachy change order for no reason
This is particularly bad when you are working with GUI because the order of the hierarchy is so importance there. I am implementing a menu system with blades that stack on top of each other.
It happens to me every now and then in my main menu scene, all of a sudden when I press the play button a few random objects will shuffle in the hierarchy and I have never been able to figure out why that happens. I have tried everything to figure out the cause: removing all animations and even disabling every single object in the scene and still those same objects will change their position in the hierarchy as soon as hit the play button (or reload the scene).
I can even reproduce this without pressing the play button:
I would start unity.
Load my project.
Open the scene.
Arrange the hierarchy in the order that things need to be.
Save the scene and Save the project
Close unity.
Then when I reopen my scene back up after all that: objects in the hierarchy are in a different/wrong order.
The only workaround I have found so far is to try to figure out which objects are the ones getting moved around, duplicating them, deleting the originals and fixing all the broken references. This is not a scale-able once your menus start becoming more complex.
Does anyone know an actual cause or solution to this problem? Reinstalling unity did not help :(
Here is an example of what I am seeing:
I just ran into this problem also, and it was after replacing existing game objects with a prefab by alt+dragging the prefab from the project onto the game object in the hierarchy. The replaced objects would get their order reset to be the first child when entering/exiting play mode or reloading the scene. Luckily there were only a few and the workaround mentioned of deleting the original and replacing it worked for those. Still a very annoying problem.
Answer by porters · Jun 09, 2017 at 05:17 PM
@gdwf I've been experiencing the same issue. I believe it could be related to one or both of the following:
gameobjects with names that contain the "_" character - generally preceding other text i.e. _myObject
nested prefabs
I have been getting around this issue with a simple script workaround. If you find a better solution then please let me know. The script I am using is below - it simply sets the sibling index of the transform the script is attached to. The script runs at design and runtime so your transforms should stay ordered how you want them all the time.
Hope this helps
[ExecuteInEditMode()]
public class SiblingIndex : MonoBehaviour
{
public int index;
private void Awake()
{
transform.SetSiblingIndex(index);
}
}
Answer by misher · May 04, 2020 at 03:50 PM
For me it was a prefab related issues. The solution was simple enough. In scene, break a prefab, change the order, drag on the prefab asset again to overwrite it with new version. Works with prefab variants as well.
Answer by HonoraryBob · May 09, 2018 at 02:37 AM
@gdfw This happened to me when I was using a prefabbed set of UI elements - i.e. one group had been saved once as a prefab, and so whenever I hit Play it would revert to the prefab's original form since I hadn't saved the new version to that prefab. I solved it just by breaking the prefab connection by moving one gameobject out of the group and then moving it back in.
This is still happening in 2019.1.14f. Did you manage to solve this non-destructively?
Answer by cmann · Dec 13, 2019 at 09:12 PM
It seems like an issue with prefabs. Not always ideal, but another solution is to wrap the prefab in a regular gameobject, which will preserve its order.
Answer by WheresMommy · Dec 14, 2019 at 06:27 AM
Did you try to put your whole Canvas in a prefab with your current prefabs in it?
Your answer
Follow this Question
Related Questions
Cant hide anything in Hierarchy IDE 2 Answers
Hierarchy in editor 2 Answers
Editor Scripting Input Problem OnGUI() 1 Answer
Unity Preferences' Large Heading Font 2 Answers
Strange Hierarchy Lag 1 Answer