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
@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);
}
}
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.
@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.
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.
This is a total bummer with nested prefabs. With a nested prefab I’m not able to save the order of where scripts reside on a gameobject, In some cases having my main script at the bottom calls for extra measures on excution. Total crap! Only solution is to break the prefab which makes nested prefabs a total waste. A huge problem.