- Home /
Exposed variable is null in Awake
I have an exposed variable that I attach to in the Inspector. But when I run the game Unity throws a NullReferenceException
in the script's Awake()
function. And - yes, I am sure the variable is attached. Also, I've tried Start()
instead and the same happens.
Even more weird, after Awake()
(i.e. Update()
) the variable is set properly and the whole script/game seems to be running fine, like there was never any error. Yet one is thrown.
Any ideas what might be the problem and how to fix it? I don't feel comfortable leaving an error in my game.
Track.cs:
using System.Collections.Generic;
using UnityEngine;
public class Track : MonoBehaviour
{
public MegaShape envCrossSectionCCW;
[HideInInspector]
public MegaShape envCrossSectionCW;
void Awake()
{
var cw = (GameObject)Instantiate(envCrossSectionCCW.gameObject); // NullReferenceException
cw.transform.parent = envCrossSectionCCW.transform.parent;
cw.name = "EnvCrossSectionCW";
envCrossSectionCW = cw.GetComponent<MegaShape>();
}
}
Edit: When I try to debug the code the debugger stops at the commented line and when I check the value of envCrossSectionCCW
it is null
. However, when I try to proceed to the next line of code in the debugger it jumps... again to the same line of code (like it resetted or sth) and this time envCrossSectionCCW
isn't null
anymore. From this point the debugging proceeds without a problem.
EDIT: Today I've discovered that my Awake()
is called twice(!) for some reason (that debugger behaviour was just that - it went through the code twice, first time ending in an error). The GameObject
with Track
script is stores in Resources
and instantiated like that:
(GameObject)Instantiate(Resources.Load("TrackMountainsForest"));
I'm pretty sure I have only 1 object with this script attached in the entire scene. Any idea why Awake()
gets called twice?
Thanks! And which line cases the NullReferenceException? Which variable does not set properly?
I'm not 100% sure, but are you definite that there is parent in all of the objects, if everything works sounds like no parent is found and null reference is thrown?
I've commented the line that throws the exception. envCrossSectionCCW
is null. Also, envCrossSectionCCW
has a parent.
I've also added a description of what happens when I try to debug the code. I'm not sure if the debugger can be trusted here (I'm using Visual Studio 2012) but this is what it does.
Answer by brunopava · Jun 30, 2014 at 11:46 AM
The var keyword is used on Javascript, to declare variables with C# you simply state the type of it.
//Your code
var cw = (GameObject)Instantiate(envCrossSectionCCW.gameObject);
//My code
GameObject cw = GameObject.Instantiate(envCrossSectionCCW.gameObject,
positionVector,
rotationVector) as GameObject;
Var in c# and in this code is absolutely legal: http://msdn.microsoft.com/ru-ru/library/bb383973.aspx
What Denvery said + just to be sure I've checked - specifying the type explicitly doesn't make the error go away.
yeah C# has allowed inferred variable declaration since c# 3.
Only exception is if you're just declaring the name of the variable to instantiate it later.
GameObject myThing;
myThing = GameObject.Instantiate(...);
*edited
"var" is not a dynamic variable. It's not dynamic as in "it can change its type dynamically". It's just a type inferred variable declaration. Implicitly typed variables can only be declared inside methods and not inside class / type declarations.
A dynamic variable in C# would be a variable of type "object". Besides that there is the type "dynamic". However it comes with runtime overhead and generally makes code harder to read and debug-
You did realise that this question and comment is over 6 years old, right?
Your answer
Follow this Question
Related Questions
Issue with scripts, error code "script needs to derive from monobehaviour" 0 Answers
Problem with attaching scripts to objects 1 Answer
Playables animation start delay 0 Answers
How do I change component properties without overriding them with parent component? 1 Answer
Avoid using MonoBehaviour just for StartCoroutine or WWW load 0 Answers