My panel dont close

Asked

Viewed 20 times

-2

im Tring to makeI’m trying to make a button that closes the hack_panel but it didn’t work

 public class Level1_Controller : MonoBehaviour {
 //Variables
 public Button Hack;
 public Button Develop;
 public Button Research;
 public Button SQ;
 public GameObject Hack_Panel;
 public bool isOpened;

 // Use this for initialization
 void Start () {
     isOpened = false;
     Hack.onClick.AddListener(Goto_hack);
     Develop.onClick.AddListener(Goto_develop);
     Research.onClick.AddListener(Goto_research);
     SQ.onClick.AddListener(Goto_sq);
 }

 public void Goto_hack() {
     if(isOpened == false) {
         Hack_Panel.gameObject.SetActive(true);
     } else {
         Hack_Panel.gameObject.SetActive(false);
     }

 }

 public void Goto_develop() {

 }

 public void Goto_research() {

 }

 public void Goto_sq() {

 }
 // Update is called once per frame
 void Update () {
     Goto_hack();
     Goto_develop();
     Goto_research();
     Goto_sq();
 }
}

1 answer

0


Maybe because you never update the variable isOpened if the Update() runs each frame and it runs the Goto_Hack(), isOpened will always be false... Besides, it doesn’t make any sense for you to perform all the click methods on each frame...

whereas the desired behaviour is that the Hack_Panel open and close by clicking the button Hack, i would rename the variable isOpened for something with more meaning, for example showHackPanel manipulate it in the event of the button and the Update() would make the state control of the Hack_Panel... see example below.

 public class Level1_Controller : MonoBehaviour {
     //Variables
     public Button Hack;
     public Button Develop;
     public Button Research;
     public Button SQ;
     public GameObject Hack_Panel;
     public bool showHackPanel = false;

     // Use this for initialization
     void Start () {             
         Hack.onClick.AddListener(Goto_hack);
         Develop.onClick.AddListener(Goto_develop);
         Research.onClick.AddListener(Goto_research);
         SQ.onClick.AddListener(Goto_sq);
     }

     public void Goto_hack() {
         showHackPanel =! showHackPanel;
     }

     public void Goto_develop() {

     }

     public void Goto_research() {

     }

     public void Goto_sq() {

     }

     // Update is called once per frame
     void Update () {
        Hack_Panel.gameObject.SetActive(showHackPanel);
     }
 }
  • thank leandro

Browser other questions tagged

You are not signed in. Login or sign up in order to post.