i need help with bollean
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enableDisableShop : MonoBehaviour
{
public GameObject shopPanel;
private bool shopEnabled = true;
public void disableShop()
{
Debug.Log("you have disabled the shop panel");
shopPanel.SetActive(false);
shopEnabled = false;
}
public void enableShop()
{
Debug.Log("you have enabled the shop panel");
shopPanel.SetActive(true);
shopEnabled = true;
}
private void FixedUpdate()
{
if (Input.GetKeyDown("b"))
{
if (shopEnabled == true)
{
disableShop();
}
if (shopEnabled == false)
{
enableShop();
}
}
}
}
I want to disable the shop menu when i press B and re-enable again when i press B again. The problem it's that whenever i press B it just disables and re-enables it.
Answer by SammyDee · May 31, 2017 at 09:17 PM
You want to use an else if for the second if statement. Right now it's checking to see if shopEnabled is true, then calling disableShop() which is setting shopEnabled to false, and then it does the second if to check if shopEnabled is false, which it is because you just changed it, so it then calls enableShop() to set it true again. An else if will stop the second check from happening if the first check was true.
Answer by PizzaPie · May 31, 2017 at 11:57 PM
void Update()
{
EnableShop();
}
void EnableShop()
{
if (Input.GetKeyDown(KeyCode.B))
{
shopPanel.SetActive(!shopPanel.activeInHierarchy);
Debug.Log("shopPanel is " + shopPanel.activeInHierarchy);
}
}
A wee bit shorter and more elegant. Also use Update() instead of FixedUpdate() for input checks unrelated to physics.
Your answer
![](https://koobas.hobune.stream/wayback/20220612123225im_/https://answers.unity.com/themes/thub/images/avi.jpg)