Getting desperate with the UI Text.

Hi,
First of all this is my first post (So far I’ve been able to manage everything by myself ).

So I am trying do display a second counter on my game. So far everything perfect, the code even works really nicely.

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

public class StatsCode : MonoBehaviour {

public static float score = 0f;
public Text text;

// Use this for initialization
void Awake () {
text = this.gameObject.GetComponent();
text.text = null;

}

// Update is called once per frame
public void Update () {
score += Time.deltaTime;
text.text = score.ToString(“F0”);
}
}

The problem comes when I run the game, I can’t avoid getting this error:

NullReferenceException: Object reference not set to an instance of an object
StatsCode.Awake () (at Assets/Scripts/StatsCode.cs:14)

I’ve tried everything so far… does anyone have an idea about how can I fix this?

Thank you in advance!

This error is simply because you have not provided a reference to the Text object via the Unity Editor’s inspector. Having not set it in the inspector, when the code runs, it is null.

Also, please use the code tags (see help area of forums) when posting code for readability.

@Kurt-Dekker is right, except this line:

text = this.gameObject.GetComponent<Text>();

overwrites whatever is set in the inspector. If you have dragged a specific text component into the inspector field you don’t need this line.

edit: if you aren’t setting it in the inspector, this script needs to be on the text child object, not the canvas/gameobject etc. and you keep the line, although you can just have

text = GetComponent<Text>();

:slight_smile:

Good eyes @LeftyRighty … without the code formatting I completely missed that .GetComponent<>() call.

To add to @LeftyRighty 's post above, if you ( @Llufes ) are doing the .GetComponent<>(), then this script must be on the same GameObject that the Text component is on.

Thank you all,

I have checked everything, the gameobject is the one having the script and it is linked in the inspector… However the error keeps apearing.

The problem is suposed to be on this line:

text.text = score.ToString(“F0”);

On the “text.text” part.

I’ve encountered similar issue before. Most likely the GetComponent is returning null. Try doing some tests on “text” variable right after this line.

text = this.gameObject.GetComponent<Text>();

I don’t have a solution other than assigning the text variable through editor

Just as an advice, you should not use text as a var name, as text is already a part of build in components. Anyway, your code is working for me. I just created an Canvas->Panel->Text object, put the text component on it if it wasn’t already there and put your script on it. Just changed the score thing to 1f to test, and it counts up every second.

Make sure also that there is not some OTHER game object in your scene that has the script on it also. Sometimes a careless mouse drag will put the script on two objects.

At the start of the Awake() function,print out the name field (this.name) and make sure the error you see is on the game object you expect it to be,

Hi all,

Some times you have the answer in front of you and you can’t manage to see it. After all, @Kurt-Dekker you were right… Another gameobject had the same script… Thank you all for your help!

This is an amazing community!

You’re welcome. I remember the first time that exact bug bit me and it took hours to figure out. Now it is simply one more thing that I check whenever I start getting really weird results like “hey, there IS a Text component, I’m looking at it! What the heck is going on!?”

i was missing this.