Canvas overrideSorting not working on nested canvas?

Hi guys,

I am having a bit of a weird problem. I am writing a tutorial for our game Empires in Ruins, and to do so for each framestep of the tutorial, i bring forward the elements that can be interacted with, while hiding everything else behind a darker panel that prevents interaction.
To do so, at each new tutorial frame/step, I attach a canvas to the tutorial I want to bring to the front if it doesn’t have one already, and i change the sorting layer.
For some reason, I am having an issue with nested canvases, i.e. when I set the canvas.overrideSorting to true, it stays false. Even printing the value right after the assignment it still prints false.
Any clue on what i might be doing wrong?

if (elem.gameObject.GetComponent<Canvas> () == null) { 
                            Canvas newCanvas = elem.gameObject.AddComponent<Canvas> ();
                            newCanvas.overrideSorting = true;
                            newCanvas.sortingLayerName = "TutorialSprites";
                            elem.alreadyHadCanvas = false;
                            Debug.Log (elem.gameObject.name +" , "+ newCanvas.overrideSorting);
                        }

Cheers,
H

Little confused, but why do you need to do this with nested canvas?
I’m not sure if adding a canvas group with ignore parent would help or not, since this may only be for certain options.

I would think you could simply have a tutorial canvas with a dark image as it’s main child. Have this be on a higher layer than your normal canvas layer and then move stuff to this tutorial layer when you need it to be in front and then move it back when you’re done with it.

Also note that the UI components will render in the order they are listed, so the last child is always in front (in case you need to bring forth multiple objects).

Hi Brathnann,

here you see for example that I am bringing forward just the subpanel of that panel to highlight it. In the same way, clicking on one of those buttons, opens up another separate panel that explains bonuses and maluses.

My UIs are spread across different canvases with different spaces (world and camera mainly), and have a quite big hierarchical structure (being a strategy game you can imagine that it is quite a wide UI system). The simple fact of bringing something to front in the traditional way, causes me a lot of issues because of this subnesting. Moreover I am making it so that also sprites belonging to the world are brought forward in front of the dark panel, and for those the added canvas seems to be the only workaround i found.

Here you see the case in which i opens up a panel, not belonging to the hierarchy of the Province panel opened in the background, but using the canvases, i can still place it automatically in the right “layer”

The weird thing is that when it doesn’t work from code (in the snipped, i set the ovveride to true, then print the override value and it returns false), if i set it from the inspector with the game running, it then works perfectly

Cheers,
Emiliano

i get the same issue,maybe is is a bug,if you get answers,please let me know,thanks
ps: i get the issue on unity5.6 but it works on unity5.5

Me I actually solved by using DestroyImmediate on the canvas instead of Destroy, it is probably related to when some behaviors happen during the frame, and DestroyImmediate seems to prevent the issue at least in my case.

The problem may be related to the Active state,
First detect the active state of the target object, if false, then set it to True, and then set the override sorting level, set the settings and then set it back.

        bool oldActive = go.activeSelf;
        if(!oldActive)
        {
            go.SetActive(true);
        }

        canvas.overrideSorting = true;
        canvas.sortingOrder = order;
        canvas.sortingLayerName = "Guide";

        if(!oldActive)
        {
            go.SetActive(false);
        }
1 Like

This was the issue for me as well. It seems that the overrideSorting does not work when the game object is inactive.

1 Like

Seriously, this is still not resolve, it’s stupid that to get a prefab to show in proper order you need to go that way :frowning:

Still not fixed in 2022. But does anyone has filled a bug report on this ?

Personally, no…
Tired of:

  1. filling bug
  2. building a specific project for them
  3. waiting month
  4. finally get an answer saying that they will look into it
  5. Later, getting an answer that they don’t really understand
  6. Sometime getting an approval but need vote
  7. Wait a few year for the bug to be fix

When I have a workaround, I don’t bother with Unity bug logging. the process, when it work, make no sense. Mostly if you have a multiplayer game with a server and database component

1 Like

I got the same issue too, and I found that the overrideSorting property will not set when the parent canvas of the gameObject not found! So pls check whether it has a parent canvas when the object is instantiated ?