- Home /
Question by
braydon97 · Jul 17, 2016 at 08:29 PM ·
c#unity 5unity5programmingif-statements
How do I Open/Close my option menu with one key C#
I wan't to be able to toggle on/off a option menu in my game And i'm confused on how to do that, I figured out how to open it but I don't know how to close it with the same key.
public Canvas IngameOption;
// Use this for initialization
void Start () {
IngameOption = IngameOption.GetComponent<Canvas>();
IngameOption.enabled = false;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("escape"))
{
IngameOption.enabled = true;
}
}
}
Comment
Best Answer
Answer by TBruce · Jul 17, 2016 at 08:38 PM
It can be done like this
public Canvas IngameOption;
private bool menuEnabled = false; // call this whatever you want
// Use this for initialization
void Start ()
{
IngameOption = IngameOption.GetComponent<Canvas>();
menuEnabled = false;
IngameOption.enabled = menuEnabled;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown("escape"))
{
menuEnabled = !menuEnabled;
IngameOption.enabled = menuEnabled;
}
}
I added in the code and it says that "menuEnabled" does not exist in the current context any ideas of why that would be?
Try specifying the type of variable: public bool menuEnabled = false;
derp, that worked thanks im still bad at this lol
This works however it's better if you use IngameOption.enabled = !IngameOption.enabled, it's the same thing but more efficient and less work.