1

I have created a Unity script which changes multiple text's every frame. Now I've dragged it onto the script, and everything is working. But when I drag the script onto the EventSystem and Drag and Drop the Text onto it, there is an error "Object Reference not set to an instance of an object". How can I fix it: Here is the script:

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class textChange : MonoBehaviour
{
    public TextMeshProUGUI text;
    //public Text costTxt;
    public TextMeshProUGUI addTxt;
    public int coins;
    public int costs;
        void Start()
    {
        text = GetComponent<TextMeshProUGUI>();
        //costTxt = GetComponent<Text>();
        addTxt = GetComponent<TextMeshProUGUI>();
    }

    Update is called once per frame
    void Update()
    {
        coins = Mathf.RoundToInt(PlayerPrefs.GetFloat("coins"));
        text.text = "$ " + coins.ToString();
        //costTxt.text = Mathf.RoundToInt(PlayerPrefs.GetFloat("cost")).ToString();
        addTxt.text = Mathf.RoundToInt(PlayerPrefs.GetFloat("add")).ToString();
    }
}
Tentaquod
  • 11
  • 2

1 Answers1

0

You are setting text in start using GetComponent, and since you moved your script to a new gameobject(that I presume that does not have a TextMeshProUGUI), it will return with no reference or instance as it could not find the component.

ChilliPenguin
  • 710
  • 7
  • 14