Hi. I know there are many questions on NullReferenceException, but each have different code. I would like to know what I am missing in mine. This is testing code my project. The first script (MoneyScript) goes onto a text object-
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MoneyScript : MonoBehaviour {
public int Money;
Text MoneyText;
BuildingUpgrade buildingUpgrade;
// Use this for initialization
void Awake () {
MoneyText = GetComponent <Text> ();
Money = 1000;
}
// Update is called once per frame
void Update () {
MoneyText.text = "$ " + Money;
}
public void doUpgrade(){
if (Money >= buildingUpgrade.UpgradeCost) {
Money -= buildingUpgrade.UpgradeCost;
} else {
Debug.Log("You do not have enough money to do the upgrade");
}
}
}
And this is the script for the value that the upgrade has -
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BuildingUpgrade : MonoBehaviour {
public int UpgradeCost = 500;
}
The text object is dragged onto the button onClick() and I access the method inside it doUpgrade()
But I get this error : NullReferenceException: Object reference not set to an instance of an object
MoneyScript.doUpgrade () (at Assets/Scrpits/MoneyScript.cs:24)
Why does the error come up and why can’t I subtract that amount from money?