I have not been using unity to program for very long so forgive me if this is a simple fix. I need to get the value of a method and display it in a prefabs text box.
Right not the code reads
public class Turret : MonoBehaviour
{
public static Turret main;
[SerializeField] private int baseUpgCost = 75;
public int GetNewUpgCost()
{
return Mathf.RoundToInt(baseUpgCost * Mathf.Pow(level, 2f));
}
}
In the script with the method and
public class UpgradeUIScript : MonoBehaviour
{
[SerializeField] TextMeshProUGUI cost;
private void OnGUI()
{
cost.text = "Cost: " + Turret.main.GetNewUpgCost().ToString();
}
}
in the prefab where I am trying to update the text.
However to code returns “NullReferenceException: Object reference not set to an instance of an object
UpgradeUIScript.OnGUI () (at Assets/UpgradeUIScript.cs:29)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)”
Thanks for any help, again sorry if this is a simple fix I’m very new to programing but I haven’t any similar questions online with answers.
SomeStupidSheep GD is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.