I want to make a gui text that appears when you press the G button and disappears when you press the G button again . Here is the code to make the box appear, but I can't figure out the disappearing part I would appreciate the help
You can just toggle the value when the button is pressed.
if (Input.GetKeyDown(KeyCode.G))
{
showText = !showText;
}
Comment code repy: Trust me, it works...
using UnityEngine;
public class GUIToggle : MonoBehaviour
{
bool showText = false;
void Update()
{
if (Input.GetKeyDown(KeyCode.G))
showText = !showText;
}
void OnGUI()
{
if (showText)
GUILayout.Label("Text");
}
}
And for sake of completeness, the same in JS:
var showText : boolean;
function Update ()
{
if (Input.GetKeyDown(KeyCode.G))
showText = !showText;
}
function OnGUI()
{
if (showText)
GUILayout.Label("Text");
}