NullReferenceException for SetActive in C#

I cant figure out why “NullRefenceException: Object reference not set to an instance of an object”

The error referring to the the this code: panel.SetActive(false);
Also, i have tried this method: panel.gameObject.SetActive(false);
both of this will give the NullReferenceException.

I need to set the PanelLabel to false at the beginning, and when the player click “1First Person Controller” it will set the panelLabel to active.

Here’s my code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class ListObject : MonoBehaviour {
    GameObject[] allObjects;
    string appendlist = "";
    int y = 40;
    GameObject panel;// nama, colour, quantity;


    void Start ()
    {
        allObjects = UnityEngine.Object.FindObjectsOfType<GameObject> ();
        Debug.Log ("Total Length: " + allObjects.Length.ToString ());
        Debug.Log ("LIST OBJECT");

        panel = GameObject.Find("PanelLabel");
        panel.SetActive (false);
        //panel.gameObject.setActive(false);
    }

    // +++++++++++++++++ button by OnGUI. Jadi! +++++++++++++++++++++++++
    void OnGUI()
    {
        // Make a background box
        GUI.Box(new Rect(10,10,200,25*allObjects.Length), "Loader Menu");

        //for loop to get the list object
        for(int x=0; x<allObjects.Length; x++)
        {
            appendlist = allObjects[x].name;

            //auto-generate button position
            if(GUI.Button(new Rect(20,y+(x*20),180,20), appendlist))
            {
                //button will do something
                buttonListener(allObjects[x]);
            }
        }
    }

    void buttonListener(GameObject buttonName)
    {
        Debug.Log ("Button Name:: " + buttonName.name);
        panelButton (buttonName.name);
        //if name==A, then popup properties A >> runtime properties
    }
    // +++++++++++++++++++ END of button by OnGUI +++++++++++++++++++++++++++

    void panelButton(string propertiesName)
    {
        //if statement
        if(propertiesName == "1First Person Controller")
        {
            //panel.SetActive(true);
            panel.gameObject.SetActive(true);
            Debug.Log("lalallalala");
        }
    }
}

Is PanelLabel disabled in the Editor when you run the game? That’s what it looks like in your screenshot.

GameObject.Find() only finds active objects, so it’s most likely returning null.

No, i screenshot this when i played this. I disabled this at the Start();

There’s no guarantee that one script’s start function is going to run before another one’s start function. If you want to get to the PanelLabel with Find, then do it in Awake, or just drag the reference over in the inspector and assign it to a public variable in the other script. PanelLabel has to be enabled to find it.

2 Likes

oh i see. im trying not to drag the reference over in the inspector, cause i might want to use lots of prefabs (also i am trying to simplify it - in the process). I choose to put in Awake(), and it does work. Thanks a lot.