UI sort order different across scene and game view

As title says - take a look at the screenshot below in playmode; The black area is correctly rendered in the scene view but wrong in game view.
8207142--1071129--upload_2022-6-15_12-42-42.png
Changing the canvas sort order in the editor during runtime will solve this issue, but that's not really a workaround for builds.

Unity has at least half a dozen different sorting rules based on what kind of objects you are working with and how they are organized. If you want any meaningful advice at least show and tell how you have those UI elements setup. Otherwise all you can get is random guesses or copy pasted blocks of documentation.

In simplest case UI elements within each canvas are sorted by their order in transform hierarchy. Do you have any scripts which create and destroy parts of that UI or otherwise change the tranform hierachy during play mode?

You mentioned canvas sort order, is the UI you showing split across multiple canvases? Do multiple of them have same sorting order? What prevents you from assigning required canvas sort order in edit mode?

[quote=“karliss_coldwild”, post:2, topic: 884922]
Unity has at least half a dozen different sorting rules based on what kind of objects you are working with and how they are organized. If you want any meaningful advice at least show and tell how you have those UI elements setup. Otherwise all you can get is random guesses or copy pasted blocks of documentation.

In simplest case UI elements within each canvas are sorted by their order in transform hierarchy. Do you have any scripts which create and destroy parts of that UI or otherwise change the tranform hierachy during play mode?

You mentioned canvas sort order, is the UI you showing split across multiple canvases? Do multiple of them have same sorting order? What prevents you from assigning required canvas sort order in edit mode?
[/quote]

Sorry, I see the confusion without extra details. The reason for omitting details is I feel like I have a good grasp on UI in general (4 years of experience) and I’ve never seen this behavior so my conclusion is it must be a bug or some other weird edge case behaviour. Especially because the screenshot above is taken during playmode (it’s one screenshot, not two), meaning that scene view and game view are reporting two different realities, which in itself is a bug to me no matter the reason behind it.

The thing about flipping the rendering order in the editor during playmode is this;

  • From starting playmode the canvases are rendered wrong in the game view but correct in scene view (the screenshot above)

  • I check the different canvases (the transparent container with the text is on its own sub canvas with overwrite sort order)

  • I see that the sort order is set to -1 while the parent sub-canvas (the icon with the car) is set to 2

  • I toggle the -1 canvas to 1, and everything renders correctly

  • I now toggle it back to -1 and everything STILL renders correctly

  • My conclusion is something is wrong with the initialization of canvases and their render order

And yes, I guess I could make a component for toggling back and forth the sort order, but that’s not solving the root issue.

So I just tested a script that goes like this;

[ButtonMethod]
    async void Toggle()
    {
        await Task.Delay(1000);
        canvas.overrideSorting = false;
        await Task.Delay(1000);
        canvas.overrideSorting = true;
    }

to toggle the canvas during play mode - but this DOES NOT WORK. E.g. I can't make components to hacky fix this issue - I HAVE to toggle "override sorting" manually to fix this bug, which of course is unacceptable for builds.

Here's a gif of the issue;
8209917--1071573--ui sorting issue.gif

Also, LayoutRebuilder.ForceRebuildLayoutImmediate(); doesn't help

That indeed doesn't seem right. Especially if increasing sort order from -1 to 1 makes it move to to the back since lower numbers are supposed to mean more towards back. Now I know what you meant by changing canvas sort order an I also I wouldn't want that since -1 and 1 are both supposed to be behind 2.

Just to be clear in the video above you toggled sort order for parent subcanvas, the one with icon and sorting order 2 not the text box which is supposed to have -1 ?

1 Like

[quote=“karliss_coldwild”, post:5, topic: 884922]
That indeed doesn’t seem right. Especially if increasing sort order from -1 to 1 makes it move to to the back since lower numbers are supposed to mean more towards back. Now I know what you meant by changing canvas sort order an I also I wouldn’t want that since -1 and 1 are both supposed to be behind 2.

Just to be clear in the video above you toggled sort order for parent subcanvas, the one with icon and sorting order 2 not the text box which is supposed to have -1 ?
[/quote]
Correct - I noticed that it doesn’t matter which canvas I change (as long as it’s one in the hierarchy with the problem) and it also doesn’t matter which option I change (sort order, override sorting, or even additional shader channels) - there must be some kind of UI refresh that is only running when interacting with the inspector, which doesn’t trigger from script.

Bug report CASE IN-7279

This is unity 2021.3.4f1 in case anyone is wondering

1 Like

I also encountered the same issue (unity 2021.3.9f1). I could not find the bug report on the issue tracker. Have there been any news?

2 Likes