Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

SetActive() Issue

Discussion in 'UGUI & TextMesh Pro' started by irdc, Nov 30, 2014.

  1. irdc

    irdc

    Joined:
    Mar 23, 2013
    Posts:
    16
    Greetings.

    Thanks for the hard work on the new GUI system.

    One issue I noticed is that the system is a little harder to manage from code than the previous system.

    Basically I want to manage opening and closing panels via script as opposed to through the editor. The main problem that I have run into is that it does not appear possible to activate an inactive panel via script. Consider the following:

    Code (CSharp):
    1. GameObject panelObj;
    2. panelObj = GameObject.Find("GameModePanel");
    3. panelObj.SetActive(true);
    This will return a NullReferenceException even though it is possible to use SetActive(true) via the editor's OnClick(). Note that the code works fine if the panel is set to active and then SetActive(false) is used.

    The workaround to this seems to be either place the panel somewhere off the canvas and then move it via transform when opening a closing a panel or attach a Canvas Group component to the panel and then adjust the alpha and interactable.

    The problem with both approaches is that the engine still draws the panel as long as it is active, thus performance is impacted on games using a lot of GUI elements.

    Is this working as intended? Not being able to active a panel from script seems a little ... limited.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    A null reference exception means there was no object found. SetActive works fine, but you need to use it with an existing object. GameObject.Find does not find inactive objects and never has; you can store a reference instead of using Find (which is generally better coding practice anyway).

    --Eric
     
  3. irdc

    irdc

    Joined:
    Mar 23, 2013
    Posts:
    16
    Thanks for the response, Eric!

    SetActive does work, but it works better with a button's OnClick component since it is possible to activate an object that has been set to inactive in the editor. This doesn't appear to be easily done via code since, unless mistaken, I generally need to use Find in order to create the reference in the first place.

    This works:

    Code (CSharp):
    1.  
    2.  
    3. GameObject[] guiObjectArray = new GameObject[5];
    4.  
    5. void Start () {
    6.      this.guiObjectArray[1] = GameObject.Find("GameModePanel");
    7.      this.guiObjectArray[1].SetActive(false);
    8. }
    9.  
    10. public void OpenPanel(int panel) {
    11.      this.guiObjectArray[panel].SetActive(true);
    12. }
    13.  
    ... but I can't set the panel to inactive in the editor since Find till return a null reference.
     
    Last edited: Nov 30, 2014
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    For existing objects in the scene, you can just drag references onto public variables. For creating objects via code, you already have a reference when you create it, so store that.

    --Eric
     
  5. irdc

    irdc

    Joined:
    Mar 23, 2013
    Posts:
    16
    Oh, awesome. Didn't know that I could create a reference by dragging the object in the editor. I mean, I guess I did, but I am used to doing everything in code.

    Creating a public array of objects and dragging the panel to it does indeed allow me to activate panels that are inactive at runtime.

    Thanks!
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Yeah, making a public reference is generally preferable, since it doesn't rely on strings (no possibility of typos), and you can change the reference to other objects without having to touch the code. Plus the whole "inactive object" thing.

    --Eric
     
    irdc likes this.