How to update a Text Component Correctly?

Hi! I’m trying to build a tycoon and I have a text component that shows the price, and I get this error:

The funny thing is that it works perfectly for the Balance and I get no error there…

This is the code for the balance:

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

public class pointCounterGamesMenu : MonoBehaviour
{
    private Text totalPoints;


    private void Awake()
    {
        totalPoints = transform.Find("balanceText").GetComponent<Text>();
    }

    // Start is called before the first frame update
    void Start()
    {
        totalPoints.text = "Balance: " + System.Environment.NewLine + Score.GetTotalPoints().ToString();
    }

    // Update is called once per frame
    void Update()
    {
        totalPoints.text = "Balance: " + System.Environment.NewLine + Score.GetTotalPoints().ToString();
    }
}

And this is what I did in the editor:

It also has a “balanceText” text component, which is changed through my code.

Here’s the Code for the TycoonTextChanger:

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

public class TycoonTextUpdater : MonoBehaviour
{

    private Text tycoon1PriceText;
    private Text tycoon1IncomeText;
    private Text tycoon1UnitsText;

   

    private void Awake()
    {
        tycoon1PriceText = transform.Find("Tycoon1PriceText").GetComponent<Text>();
        tycoon1IncomeText = transform.Find("ProductionPerDayTextTycoon1").GetComponent<Text>();
        tycoon1UnitsText = transform.Find("UnitsOwnedTextTycoon1").GetComponent<Text>();


    }

    // Start is called before the first frame update
    void Start()
    {
        tycoon1PriceText.text = "BUY: " + TycoonBasic.tycoon1.price.ToString();
        tycoon1IncomeText.text = "Income: " + (TycoonBasic.tycoon1.unitCount * 3).ToString() + "/day";
        tycoon1UnitsText.text = "Units: " + System.Environment.NewLine + TycoonBasic.tycoon1.unitCount.ToString();
        Debug.Log("Start Works");
    }

    // Update is called once per frame
    void Update()
    {
        tycoon1PriceText.text = "BUY: " + TycoonBasic.tycoon1.price.ToString();
        tycoon1IncomeText.text = "Income: " + (TycoonBasic.tycoon1.unitCount * 3).ToString() + "/day";
        tycoon1UnitsText.text = "Units: " + System.Environment.NewLine + TycoonBasic.tycoon1.unitCount.ToString();
    }
}

And in the editor, I tried attaching it to each GameObject that has the text as a child and also to the main GameObject that has the GameObjects that hold the text.

Still not working and getting that error.

If you need more code or anything, please let me know!

Null errors are when what you’re trying to work with isn’t there. It means something is erroneous with your code, and Unity can’t proceed because you’re trying to make it work with nothing. You’re given a null reference when GetComponent or similar don’t find what you’ve specified it to look for.

The reason for this, in your case, is that you’re transform.Find isn’t finding what you’re looking for. And honestly, you really shouldn’t be using it as well. Search for stuff via string literals is fragile and any single typo or renamed object will break your code.

Instead, in this situation, you should use [SerializeField] on each of your private Text fields and assign them directly in the editor.

1 Like

There is ZERO code and ZERO forum post required for null reference error. Posting anything is unhelpful and actually just a waste of time, distracting you from solving the problem. You can solve the problem all by yourself and here is how:

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7