- Home /
Unassigned Reference Exception although it is assigned
Hello, there is an "Unassigned Reference Exception" in my game and it drives me mad. Apparently there are already many questions asked about the exact same problem but no answer was helpful for me. Now I will try my luck and ask the question myself:
I try to instantiate a prefab and set the canvas as its parent. This is an excerpt of my code:
using UnityEngine;
public class Inventory : MonoBehaviour
{
[SerializeField] Canvas canvas;
[SerializeField] GameObject somePrefab;
void Start()
{
canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
print(canvas.gameObject.name);
}
void Update()
{
print(canvas.gameObject.name);
}
public void MoveOnCanvas()
{
GameObject hoverObject = (GameObject)Instantiate(somePrefab);
hoverObject.transform.SetParent(canvas.gameObject.transform, true);
}
}
It results in this output:
The Start() callback works fine, the canvas gets printed. Also Update() works as it should. The MoveOnCanvas() method gets called when I click on a button. When MoveOnCanvas() gets called it results in the error. The variable "canvas" is assigned, otherwise the print statements wouldn't work! It even highlights the referenced object in the Hierarchy if I click on the field in the Inspector:
I can not find a solution to this problem. I can't even find the cause of it. Many questions regarding this problem mentioned there might be a second instance of the Component (in my case "Inventory") or it might be attached to the wrong GameObject. This is not the case. I implemented a static integer variable and incremented it on Start(): Start only gets called once. As long as I get it correctly, this means there only is 1 instance of my Inventory.
If I change MoveOnCanvas() to this, it works fine:
public void MoveOnCanvas()
{
GameObject hoverObject = (GameObject)Instantiate(somePrefab);
hoverObject.transform.SetParent(GameObject.Find("Canvas").transform, true);
}
How is this possible? Why can a member variable of a class can be assigned to some functions (Start() and Update() but contain null to others (MoveOnCanvas())? Maybe someone of you has a hint. I would appreciate it.
I could upload the whole class code to a Gist if you think it helps.
Answer by Harinezumi · May 23, 2018 at 03:59 PM
When you click your button, on what object does the button call MoveOnCanvas()
? Maybe there is a second Inventory
script that has not been initialized, or clicking the button creates an Inventory
script in that moment, and so its Start()
has not yet been called ( Start()
only gets called just before the first Update()
for the given script); and when you see the error, it has already been initialized.
But I'm just guessing, this really is a weird error.
Your guess was crucial for the problem solving! Thank you! The button component has a reference to the Inventory prefab and not to the initialized inventory. The inventory prefab does not have an assigned canvas variable since this happens at runtime. I haven't implemented a working solution yet but in theory this has to be the cause! Damn it! Sometimes you miss the forest for the trees...
Cool, I'm glad I could help! :)
I converted the comment into an answer, as it turned out to be more relevant than expected.