Null Reference Exception only in second method
I've had several Null Reference Exception errors during the development of this project, and I felt like I had a handle on why they were happening, but this one has me stumped. I have a prefab (called specialFind) that gets instantiated several times throughout the scene on load. It has the following script on it (note that I took out several variables to save on space here, but if my comments aren't clear, I can provide the full code):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class SFdata : MonoBehaviour {
//public variables that receive data at instantiation
public int ID; //there are several others
CamMouseLook mouselook;
public GameObject viewer;
public Canvas viewerCanvas;
//Record Viewer Fields declared
private Text viewerID; //again, several others exist
void Start()
{
viewer = GameObject.Find("recordViewer");
viewerCanvas = viewer.GetComponent<Canvas>();
viewerCanvas.enabled = false;
Debug.Log("viewer is disabled");
mouselook = GameObject.Find("Main Camera").GetComponent<CamMouseLook>();
//assign correct components to record viewer fields
viewerID = viewer.transform.Find("Panel/Header").GetComponent<Text>();
//again, there are others
}
void OnMouseDown()
{
//when object is clicked on, open the dataViewer window and show the relevant data
mouselook.enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
viewerCanvas.enabled = true;
Debug.Log("Viewer is enabled");
//set values for viewer
viewerID.text = "Special Find: " + ID.ToString();
//several others
}
public void closeRecordViewer()
{
SFdata SFdata = this;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//next line throws Null Reference Exception
mouselook.enabled = true;
viewerCanvas.enabled = false;
}
}
So right now, things work as intended for the first two steps. If you click on an instance of specialFind it will open the data viewer and display the proper data. The problem happens when I click the 'X' button I made (grandchild of viewerCanvas), which has an OnClick event that calls closeRecordViewer(). For some reason, Start() and OnMouseDown() can use the variables like mouselook and viewerCanvas, but closeRecordViewer() gets a NRE when trying to access them. I thought that, being inside the same class, all methods would have access to these variables, but maybe I'm wrong? Can anyone tell me why this is happening?
Also, if you've got any criticism about the structure of the code, I'd very much appreciate that as well. But I mostly just want to get this thing working.
Have you tried setting those variables on the inspector rather than using Find()?
$$anonymous$$y best guess would be that you are calling closeRecordViewer
before Start
. But since I can not see how/when you are instantiating SFdata object and how/when you are calling closeRecordViewer
, I can't say for sure.
So, make sure you are calling closeRecordViewer
after Start
was called already.
Answer by Glurth · Jan 17, 2017 at 05:20 PM
Sounds like you have two components, of the same class (SFdata), on two or more objects in the Scene. Since you declared the variables without the "static" modifier, each instance of this class will store it's own copy of these variables. This makes sense when your class is say.. a text field, and need to store a unique string. However, if you want ALL instances of your class to be able to access the SAME variable, you will need to define the variable as static.
e.g.
public class SFdata : MonoBehaviour {
//public variables that receive data at instantiation
public int ID; //looks like this one SHOULD be stored in each instance.
static CamMouseLook mouselook; //I suspect this is one you wan to make static, and share this one variable among all instances of SFData
public GameObject viewer; // not sure if you'd want this to be the same of all instances....
static public Canvas viewerCanvas; // I'd guess this one should be static too.
....
}
If you choose to NOT make them static, you will need to ensure that every instance has it's variables setup properly when created or enabled.
e.g.
OnEnable()
{
if(!mouselook || !viewerCanvas)
Debug.Log("Warning, not all variables setup on object: " + gameObject.name);
}
That's probably what was happening. After playing with it some more, I refactored and moved the closeRecordViewer() into my UI manager script, where it probably should've been all along, and this fixed the problem. But I'm glad to know what the issue was. Apparently I need to go read up on static variables and scope more. Thanks!
Your answer
Follow this Question
Related Questions
Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer
Static function not working in between screen 2 Answers
I get a "NullReferenceException" when trying to change the text of a UI text box. 1 Answer
How to send event to my canvas via script 0 Answers
Always pickup closest item? 0 Answers