Make a GUI Reappear then Disappear Using Same Key?
Hi
I am trying to create a script that will hide and show my GUI.
Script:
using UnityEngine;
using System.Collections;
public class PauseMenuBackUp : MonoBehaviour {
public GameObject GUI;
// Use this for initialization
void Start () {
HideCanvas ();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ((KeyCode.Escape)))
LoadCanvas ();
}
void LoadCanvas() {
GUI.SetActive (true);
}
void HideCanvas() {
GUI.SetActive (false);
}
}
When I run the game, I press ESCAPE then the GUI shows up. But when I press ESCAPE again it does not go away. Please help.
Thanks!
Answer by DThaiPome · Feb 15, 2016 at 12:31 PM
I would create a variable that stores whether or not the canvas is active. Then, if the canvas is active, have the button deactivate it, and vice versa. Here: using UnityEngine; using System.Collections;
public class PauseMenuBackUp : MonoBehaviour {
public GameObject GUI;
bool canvasActivated;
// Use this for initialization
void Start () {
HideCanvas ();
canvasActivated = false;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ((KeyCode.Escape)))
if (canvasActivated == false) {
LoadCanvas ();
canvasActivated = true;
} else if (canvasActivated == true) {
HideCanvas ();
canvasActivated = false;
}
void LoadCanvas() {
GUI.SetActive (true);
}
void HideCanvas() {
GUI.SetActive (false);
}
}
I'm not exactly sure how to link the status of the GUI to the boolean so that it updates by itself, but you could find that after a little bit of research. For now, this script should work as long as you change the condition of the boolean upon each switch.
Your answer
![](https://koobas.hobune.stream/wayback/20220612064129im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loading Game Crash 1 Answer
Multiplayer Hiding Layer just for Local Player 0 Answers
How to change GUI label size 1 Answer
Moving GUI Label through key board? 1 Answer
Connect C# script with GuiText 1 Answer