I have many UI panel and smaller pieces that are loaded dynamically when needed.
But very often, even if I have a Canvas component on them, with a specific Sort Order, UI elements that should appear on top, are rendered below.
If in the editor I just uncheck and check again the “Override Sorting” checkbox of the Canvas component, the the everything is correct.
As a workaround I have now to increase by code the Sort Order value of new elements to force Unity to evaluate that value.
This really seems a last longing bug. But I can’t find anybody having the same problem online. So maybe I’m missing something obvious.
I’m using an Overlay canvas as root, and then I load every page/popup inside it.
Each popup has its own Canvas to override the sorting order and render over the pages below.
I add a canvas also to smaller elements that I have to add dynamically inside the popups, otherwise they are rendered below the popup background, even if they are set as children of a popup element.
I just ran into this bug, although reproduced in a different way. It doesn’t seem to be related to prefabs, but it seems more related to the game object’s active state.
Ex:
Panel - Add canvas with override sorting on, and set sort order 10
Child panel - Add canvas with sort order 9 (so it’d draw behind the parent)
Now if I toggle the active state of the parent panel, the child panel will end up being drawn on top of the parent instead of behind. If I change its sort order in the inspector, then the sort order corrects itself.
What’s odd is in the scene view, the sort order is correct. It’s only in the game view that it appears incorrect.
Yes you’re right. It’s enough to enable later a game object with a different sorting order to reproduce this issue.
Also I found that is enough to assign the same sorting order value (and not to increase it) to the canvas to force an internal refresh.
I’ll try to fill a bug report then, but if you already have a simple project to send, you could be faster than me.