- Home /
NullReferenceException
Hi all,
I'm fairly new to this so forgive my inexperience.
I am trying to create a timer in unity. I have two scripts. The first is the GameControllerScript which creates a variable gameTime = Time.time. This will start when the game is launched. In the levels (or tracks) I want to start a different timer called track time which only begins when the player starts the track so I have a different script for each level.
Here are the two scripts
#pragma strict
public var gameTime : float;
function Start ()
{
gameTime = Time.time;
}
function Update ()
{
}
and the second one
#pragma strict
private var gameControllerScript : GameControllerScript;
private var trackTime : float;
gameControllerScript = GetComponent(GameControllerScript);
function Start ()
{
}
function Update ()
{
trackTime = Time.time - gameControllerScript.gameTime;
}
This is the error that I am getting.
NullReferenceException: Object reference not set to an instance of an object TrackControllerScript.Update () (at Assets/TrackControllerScript.js:15)
Please could someone explain why I'm being told that it is a Null reference.
Regards
Answer by Noah Dyer · Jan 14, 2015 at 07:24 PM
You can't set gameControllerScript as a "class" variable [not sure what the correct nomenclature for such a variable is in JavaScript]. You have to do it in a function. in this case I'd recommend Start() :)
Really? I don't know a lot about Unity script but as it has a file level execution scope my assumption was that all the components are created and attached before that scope is evaluated...
To clarify, I'm suggesting you move gameControllerScript = GetComponent(GameControllerScript); to the Start() function.
Ok I've changed the code to the following
#pragma strict
private var trackTime : float;
private var gameControllerScript : GameControllerScript;
function Start ()
{
gameControllerScript = GetComponent(GameControllerScript);
}
function Update ()
{
trackTime = Time.time - gameControllerScript.gameTime;
}
however I'm still getting the same error. I've tried moving the gameTime = Time.time; to the update function in the first script as it looked like it wasn't actually counting.
I'm wondering if I haven't put the scripts in the right folder or something.
Are both of your scripts attached to the same object? If not, the GetComponent() call will return null.
If they're not on the same object, you'll to need to use GameObject.Find(), or create a relationship between the GameController and your timer using class variables and the editor.
http://docs.unity3d.com/ScriptReference/GameObject.Find.html
It'll look something like:
var gcObject = GameObject.Find("gameControllerObjectName");
gameControllerScript = gcObject.GetComponent(GameControllerScript);
Answer by Jeff-Kesselman · Jan 14, 2015 at 07:24 PM
Add a Debug.Log in Update and print the value of gameControllerScript.
My strong suspicion is that it is null.
If so, check to make sure the object that script is on really has a GameControllerScript component as well.