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");
}
}
}
