- Home /
NullReferenceException
Can anyone explain to me why I am getting this error NullReferenceException object reference not set to instance of an object. Im getting this issue in my game so I came to the live training and started watching the Saving and loading data tutorial. Followed it step by step and I am getting the same error i had in my own game.
Here is my GameControl Script attached to a GameControl object
using UnityEngine;
using System.Collections;
public class GameControl : MonoBehaviour {
public static GameControl control;
public float health;
public float exp;
void OnGUI()
{
GUI.Label (new Rect (10, 10, 100, 30), "Health" + health);
GUI.Label (new Rect (10, 40, 150, 30), "EXP" + exp);
}
}
Here is the adjust script attached to the main camera
using UnityEngine; using System.Collections;
public class AdjustScript : MonoBehaviour {
void OnGUI()
{
if(GUI.Button(new Rect(10,100,100,30),"Health up"))
{
GameControl.control.health += 10;
}
if(GUI.Button(new Rect(10,140,100,30),"Health down"))
{
GameControl.control.health -= 10;
}
if(GUI.Button(new Rect(10,180,100,30),"Exp up"))
{
GameControl.control.exp += 10;
}
if(GUI.Button(new Rect(10,220,100,30),"Exp down"))
{
GameControl.control.exp -= 10;
}
}
}
That is it, super simple and identical to the code in the video. I am getting the error but he is not. Please Help!! Thank you
Nothing is initializing GameControl.control. Have you tried dragging the GameControl object into the inspector property for control?
He wouldn't be able to drag it into the inspector property since it's a static variable and those aren't serialized in the inspector.
Oh, didn't know you couldn't but that would make sense I suppose. He needs to initialize the control object for sure though.
Answer by Kiwasi · May 13, 2014 at 07:32 PM
Add these line to your GameControl
void Awake (){
control = this;
}
Basically you are not assigning a reference to control.
I added this and now it works, could anyone tell me why :)
void Awake()
{
if(control == null)
{
DontDestroyOnLoad(gameObject);
control = this;
Its the same as Bored$$anonymous$$ormon did but it checks first. The DontDestroy does not contribute to the effect.
The key line is
control = this;
You simply need to place this line anywhere before (in execution order) you try to reference it.
Execution order runs something like this
Every Awake() runs
Every Start() runs
Update() runs every frame
Each time a GameObject is instantiated it runs its Awake() and then Start() before the current Update() countinues
In practice this means:
Awake() should contain all code needed to initialise itself. Don't refer to any other GameObjects during Awake() because they might not exist yet.
Start() should contain everything needed to initialise a GameObject in relation to other GameObjects. Anything set up in awake will be available here.
Update() contains the code you run every frame. Avoid anything with a high performance cost here.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144949im_/https://answers.unity.com/themes/thub/images/avi.jpg)