Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity UI obj.SetActive(false) not working

Discussion in 'UGUI & TextMesh Pro' started by rusoaica, Apr 21, 2018.

  1. rusoaica

    rusoaica

    Joined:
    May 27, 2015
    Posts:
    3
    Hello! Simplified, I have a UI menu in which I have a parent object called AppMenu, and some child objects which are the actual menu items (File, Edit, etc). On the AppMenu object, I have a script attached, through which I expose an array of GameObject[] type, which I then instantiate in the Editor with the child Menu Items objects. Then, in my code, I am trying to show/hide these objects, based on whatever menu button was pressed. The showing of the menu items works fine. However, hiding them does not work at all. Here is a preview of how my Hierarchy looks like:

    unity setactive.png
    and my code is as following (removing the unrelated parts):

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class AppMenu : MonoBehaviour
    5. {
    6.     public Button[] Buttons;
    7.     public GameObject[] Popups;
    8.     private bool isMenuOpen = false;
    9.     private bool ButtonClicked;
    10.     private bool isFileOpen = false;
    11.  
    12.     public void OpenMenu(string func)
    13.     {
    14.         if (!isMenuOpen)
    15.         {
    16.             foreach (GameObject obj in Popups)
    17.                 obj.SetActive(false);
    18.             foreach (Button but in Buttons)
    19.                 but.transform.GetComponent<ButtonToggleEffect>().Unlit();
    20.         }
    21.         switch (func)
    22.         {
    23.             case "File":
    24.                 if (isFileOpen)
    25.                 {
    26.                     isFileOpen = false;
    27.                     isMenuOpen = false;
    28.                     Popups[0].SetActive(false);
    29.                     Buttons[0].transform.GetComponent<ButtonToggleEffect>().Unlit();
    30.                 }
    31.                 else
    32.                 {
    33.                     isFileOpen = true;
    34.                     isEditOpen = false;
    35.                     isToolsOpen = false;
    36.                     isSymmetryOpen = false;
    37.                     isHelpOpen = false;
    38.                     isMenuOpen = true;
    39.                     Popups[0].SetActive(true);
    40.                     Buttons[0].transform.GetComponent<ButtonToggleEffect>().Lit();
    41.                     Buttons[1].transform.GetComponent<ButtonToggleEffect>().Unlit();
    42.                     Buttons[2].transform.GetComponent<ButtonToggleEffect>().Unlit();
    43.                     Buttons[3].transform.GetComponent<ButtonToggleEffect>().Unlit();
    44.                     Buttons[4].transform.GetComponent<ButtonToggleEffect>().Unlit();
    45.                 }
    46.                 break;
    47.         }
    48.     }
    49. }
    So, Popups[0].SetActive(false); doesn't seem to work, and I don't understand what am I doing wrong. In fact, I can see the File game object turning light gray in Hierarchy, and the small checkbox near its name being deselected, so the game object is indeed deactivated, it just still remains displayed.
    I did notice though that if I resize the Game tab of the editor, it disappears instantly. Could it be a UI update bug? What am I missing, or, what is the right way to hide a UI element?
    Thanks!
     
  2. rusoaica

    rusoaica

    Joined:
    May 27, 2015
    Posts:
    3
    Later Edit: It seems that in Scene View, the game object is indeed deactivated, as shown in the print screen, just not in the Game View. It is also still visible when Build and Run too.
    unity setactive.png
     
  3. karsnen

    karsnen

    Joined:
    Feb 9, 2014
    Posts:
    65
    I have exactly the same issue. Just do not understand.
     
    Airmouse likes this.
  4. karsnen

    karsnen

    Joined:
    Feb 9, 2014
    Posts:
    65
  5. Marhaikal

    Marhaikal

    Joined:
    Apr 20, 2017
    Posts:
    17
    Same issue here. Anyone found a solution?