Search Unity

Finding if a button is already clicked or not to show/hide UI panel.

Discussion in 'Scripting' started by Zer0_obscura, Aug 29, 2018.

  1. Zer0_obscura

    Zer0_obscura

    Joined:
    Nov 20, 2013
    Posts:
    13
    Hello all, trying to get a button to make a UI component set a panel as active, or not depending on if the button has already been clicked, or referencing weather or not the panel is active or not. Can someone help me straighten this out?

    public void SetVisual(bool value)
    {

    if (MobliePanel.activeInHierarchy == true) {
    MobliePanel.gameObject.SetActive(false);
    }
    if (MobliePanel.activeInHierarchy != false) {
    MobliePanel.gameObject.SetActive(true);
    }

    }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Do you mean that the button controls the panel's visibility but the initial state of the button is set from the initial visibility of said panel?
     
  3. DaemonicDreams

    DaemonicDreams

    Joined:
    Aug 23, 2018
    Posts:
    41
    Code (CSharp):
    1. if (MobliePanel.activeInHierarchy != false) //...
    That is essentially the same as asking if activeInHierarchy == true.
    "!=" means "not equal"; if a bool is not equal to false it must be true.
     
    Zer0_obscura likes this.
  4. Zer0_obscura

    Zer0_obscura

    Joined:
    Nov 20, 2013
    Posts:
    13
    Yessir, panel is initially invisible until button is pressed. Then visible, and trying to make it invisible again when pressed a second time.
     
  5. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,913
    Code (CSharp):
    1. public void ToggleVisual()
    2. {
    3.     if (MobliePanel.activeInHierarchy == true)
    4.     {
    5.         MobliePanel.gameObject.SetActive(false);
    6.     }
    7.     else
    8.     {
    9.         MobliePanel.gameObject.SetActive(true);
    10.     }
    11. }
     
    Zer0_obscura and DaemonicDreams like this.
  6. Zer0_obscura

    Zer0_obscura

    Joined:
    Nov 20, 2013
    Posts:
    13
    Thank you guys soooo much!