Hi, I’ve been trying to make a textmesh pro text update to the current value of “coins” stored in the GameController but I keep getting: “NullReferenceException: Object reference not set to an instance of an object” in line 22 (coinsText.SetText("Coins: " + coinsScript.coins); ). How can I fix this so it updates properly?
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class UpdateCoins : MonoBehaviour
{
public GameObject gameConroller;
private CoinControl coinsScript;
public TextMeshProUGUI coinsText;
// Start is called before the first frame update
void Start()
{
gameConroller = GameObject.Find("GameController");
coinsScript = gameConroller.GetComponent<CoinControl>();
coinsText = GetComponent<TextMeshProUGUI>();
}
// Update is called once per frame
void Update()
{
Debug.Log(coinsText);
coinsText.SetText("Coins: " + coinsScript.coins);
}
}