- Home /
Show and hide a canvas on a button click.
I have two canvases in my scene one is for the main menu. It has play, options and quit buttons. I would like it that when I click the options button it shows a second canvas, that starts of hidden, how would I do this?
Answer by AlanGreyjoy · Jan 03 at 01:52 AM
You can also click the plus sign on OnClick, and drag over that panel. Then on the down arrow select GameObject.SetActive
Un checked is false and Check is true.
Whatever panel you want to start off hidden, then you just uncheck it from the very very top of the inspector.
No code needed.
Once you need code, then you need to learn enums and switch statements. :)
Answer by Gooren · Jan 02 at 09:24 AM
Select button in the Inspector. On the very bottom, You have OnClick() handlers. There You can reference method of some component on specific GameObject. So use it to reference method that will know about/be able to to find both Your canvases and then You will say something like:
CanvasA.gameObject.SetActive(false);
CanvasB.gameObject.SetActive(true);
What i did is i created a new script put CanvasA.gameObject.SetActive(false); under void Start () { i then attached that to an empty game object, which i then set the OnClick() to that game object, where do i specify which canvas i want to disapear?
CanvasA.gameObject.SetActive(false);
means disabling CanvasA. So you already specified it.?
I would also not put it in the start function, because that is run as soon as the game starts. What Gooren ment, is to make a new method and link it to the OnClick() handler.
Like so:
void disableCanvas() {
CanvasA.SetActive(false);
}
void enableCanvas() {
CanvasA.SetActive(true);
}
If you wish to do it through scripting and not use the OnClick() handlers you could go for this:
void changeCanvasProperty(GameObject yourCanvas, bool disable) {
if (!disable)
yourCanvas.SetActive(false);
else
yourCanvas.SetActive(true);
}
This way you can use it as follows: changeCanvasProperty(CanvasA, false);
sorry I'm not very good at c#, I don't quite understand what I'm supposed to do, this might be asking a bit too much but could some one take me through a step by step process? The canvas is called options.
Your answer
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Follow this Question
Related Questions
UI button dosent show function 1 Answer
Canvas Button persistence 0 Answers
Right click listener - generated canvas buttons 0 Answers
Issue with UI button positioning 1 Answer
Unity UI 4.6 Canvas Enable/Disable make Accessible 2 Answers