0

I have a simple test script to display a message, but I'm getting an error of NullReferenceException on runtime. Here's my code:

Text messageText;

    void Start () 
    {
        messageText.GetComponent<Text>();
    }

    void Update () 
    {
        messageText.text = "Test";
    }

But when I try to put my more complex script, it run and changes the value of my messagebox, here's my other script:

Text expText;
    public Fighter fighter;

    void Start()
    {
        expText = GetComponent<Text>();
    }

    void Update()
    {
        expText.text = fighter.levelingSystem.currentExp + " / " + fighter.levelingSystem.maxExp;
    }

Here's my error:

NullReferenceException: Object reference not set to an instance of an object MessageText.Start () (at Assets/MessageText.cs:11)

Aldrin Ramirez
  • 129
  • 1
  • 2
  • 12

1 Answers1

0

In the first example it looks like you're trying to use messageText without initializing it, meaning that when Start() is called messageText is NULL.

Cruentus_Nex
  • 104
  • 6