- Home /
hide show canvas and ui elements at start
I want to start a game with a canvas of UI buttons that appears only when the game is done with an initialization function. I tried making the canvas inactive in the inspector to start and then using SetActive in the script, but I get a null reference.
I've read people suggesting to make a canvas group and set alpha to 0 but that the buttons are still clickable which could also crash if someone clicks the screen before everything is loaded. Isn't there an easy way to just hide show a canvas or individual UI elements?
Answer by DoTA_KAMIKADzE · Apr 12, 2015 at 12:42 AM
For that purposes I tend to create a Manager script that I add to Main UI canvas and it contains basically 2 function Show and Hide. Now what those 2 thing will do is up to you.
The concept is - if you have more than 1 UI thing to be shown/hidden (with animation or without doesn't really matter) you just throw it to an empty container, then if this is a single UI or a container you just add CanvasGroup (now if there is animation you can also add a script with animation to it).
The next steps are pretty simple:
-Hide function changes alpha to 0 and sets BlocksRaycasts to false (you can set Interactable to false as well in case you tick your UIs without raycasting).
-Show function just does the oposite to Hide function. (alpha = 1, BlocksRaycasts = true, and optionally Interactable = true, if it was previously set false in hide function).
Now if you have animation you can as well move it outside of the screen bounds if you're willing to do so, but CanvasGroup is enough to be invisible and not interactable.