In a script I’m using a static variable from another script. The app works fine, but I get this error message in every update:
NullReferenceException: Object reference not set to an instance of an object
HUD.Update () (at Assets/Scripts/HUD.cs:19)
My scripts:
Where I declaring the variables:
...
public static int score;
public static int health;
public static int money;
...
void Start()
{
score = 0;
health = 3;
money = PlayerPrefs.GetInt("Value");
...
and when I use, the another script:
...
void Update () {
TextScore.text = "" + GameManager.score; //this is the HUD.cs line 19, what seem in the error message
TextHealth.text = "" + GameManager.health;
money.text = "" + GameManager.money;
...
The last two days I tried to fix this problem, but nothing! I’m a little bit frustrated!
Just make a null check for everything on that line e.g. TextScore, to find out which one causes the issue. Besides that, click on the error message. It may happen that you accidentally assigned the script to another game object. By clicking on the error message, the game object that is causing the error message gets highlighted in the hierarchy.