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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

NullReferenceException for SetActive in C#

Discussion in 'Scripting' started by sarangge, Aug 13, 2015.

  1. sarangge

    sarangge

    Joined:
    Jul 29, 2015
    Posts:
    11


    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:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6.  
    7. public class ListObject : MonoBehaviour {
    8.     GameObject[] allObjects;
    9.     string appendlist = "";
    10.     int y = 40;
    11.     GameObject panel;// nama, colour, quantity;
    12.  
    13.  
    14.     void Start ()
    15.     {
    16.         allObjects = UnityEngine.Object.FindObjectsOfType<GameObject> ();
    17.         Debug.Log ("Total Length: " + allObjects.Length.ToString ());
    18.         Debug.Log ("LIST OBJECT");
    19.  
    20.         panel = GameObject.Find("PanelLabel");
    21.         panel.SetActive (false);
    22.         //panel.gameObject.setActive(false);
    23.     }
    24.  
    25.     // +++++++++++++++++ button by OnGUI. Jadi! +++++++++++++++++++++++++
    26.     void OnGUI()
    27.     {
    28.         // Make a background box
    29.         GUI.Box(new Rect(10,10,200,25*allObjects.Length), "Loader Menu");
    30.  
    31.         //for loop to get the list object
    32.         for(int x=0; x<allObjects.Length; x++)
    33.         {
    34.             appendlist = allObjects[x].name;
    35.  
    36.             //auto-generate button position
    37.             if(GUI.Button(new Rect(20,y+(x*20),180,20), appendlist))
    38.             {
    39.                 //button will do something
    40.                 buttonListener(allObjects[x]);
    41.             }
    42.         }
    43.     }
    44.  
    45.     void buttonListener(GameObject buttonName)
    46.     {
    47.         Debug.Log ("Button Name:: " + buttonName.name);
    48.         panelButton (buttonName.name);
    49.         //if name==A, then popup properties A >> runtime properties
    50.     }
    51.     // +++++++++++++++++++ END of button by OnGUI +++++++++++++++++++++++++++
    52.  
    53.     void panelButton(string propertiesName)
    54.     {
    55.         //if statement
    56.         if(propertiesName == "1First Person Controller")
    57.         {
    58.             //panel.SetActive(true);
    59.             panel.gameObject.SetActive(true);
    60.             Debug.Log("lalallalala");
    61.         }
    62.     }
    63. }
    64.  
     
    Last edited: Aug 13, 2015
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
  3. sarangge

    sarangge

    Joined:
    Jul 29, 2015
    Posts:
    11

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

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
    cinnamonjames and sarangge like this.
  5. sarangge

    sarangge

    Joined:
    Jul 29, 2015
    Posts:
    11
    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.