- Home /
Re-enabling canvas causes UI components to lose normal scaling
I have a canvas that has some images, buttons, etc.
It is used as an ending screen, so at the start of the game, it is disabled using the following code:
endCanvas.enabled = false;
At the end of the game, it is enabled using the same code:
endCanvas.enabled = true;
The canvas elements are becoming scaled after it is re-enabled.
Before game start (how it is supposed to look) After game end (canvas enabled again, elements scaled)
I found a fix, but I cannot use it.
endCanvas.gameObject.setActive (false);
And
endCanvas.gameObject.setActive (true);
I think it has something to do with the UI rebuild that is triggered when onEnable for the gameobject is called, but that's about it.
Thanks, in advance, ~NT
are you using Layouting components? if so, disable those after you're satisfied with the layouting result. In some unity versions the layouting is broken.
@n314p - Do you really need to disable the canvas? How about just make the panel a panel in some other canvas, where all the other UI elements are, and the just disable the panel (if that is possible) ? Or, alternatively make your panel have canvas group, then make it's alpha 0 and interactable false, when the panel is hidden.
Answer by FernandoHC · Aug 15, 2018 at 11:09 AM
The enabled variable is used to activate/deactivate components, in your case, you probably want to deactivate the whole GameObject where that canvas is, so you can actually use gameObject.setActive (true); and gameObject.setActive (false);