- Home /
how to make a menu open and close with the same button
my code looks like this: void Update () {
if (Input.GetKeyDown(KeyCode.Tab) && computerOn == false)
{
SuperStore.SetActive(true);
computerOn = true;
}
if (Input.GetKeyDown(KeyCode.Tab) && computerOn == true)
{
SuperStore.SetActive(false);
computerOn = false;
}
}
what i think currently happens is that when i press tab it opens and closes the menu really fast but i have no idea how to fix it please help.
btw SuperStore is the menu.
Answer by ShadyProductions · Jul 03, 2018 at 09:08 AM
What you need to do is make it an ELSE if
Because now you're excuting your first if, setting you bool to true, and then executing the other if thats not what you want.
if (Input.GetKeyDown(KeyCode.Tab) && computerOn == false)
{
SuperStore.SetActive(true);
computerOn = true;
}
else if (Input.GetKeyDown(KeyCode.Tab) && computerOn == true)
{
SuperStore.SetActive(false);
computerOn = false;
}
But you can write it much shorter.
if (Input.GetKeyDown(KeyCode.Tab))
{
computerOn = !computerOn;
SuperStore.SetActive(computerOn);
}
Answer by $$anonymous$$ · Jul 03, 2018 at 09:00 AM
Okay, so try this
Bool MenuIsUp = false;
if (Input.GetKeyDown(KeyCode.Tab) )
{
if (MenuIsUp == false)
{
SuperStore.SetActive(true);
MenuIsUp = true;
} else {
SuperStore.SetActive(false);
MenuIsUp=false;
}
I am unsure if this would work but it is the first thing that popped into my head when reading your issue
that is what the computerOn variable is for and yes I did set it to false in the start function
Answer by fghfghfg · Jul 03, 2018 at 09:50 AM
You could also do like diss if (Input.GetKeyDown(KeyCode.Tab)){ SuperStore.SetActive(!SuperStore.activeSelf); computerOn = SuperStore.activeSelf; }
much less code and way cleaner in my opinion.