NullReferenceException: Object reference not set to an instance of an object

NullReferenceException: Object reference not set to an instance of an object at StatDisplay.
I don’t know why even I copy and paste lecture code but it shows that error.
It shows on especially

private void UpdateDisplay () {
starText.text =stars.ToString();
}

for this sentence.

I attached script to LevelCanvas-Stars with text

I almost give up for this issue…

If there are any body to let me know… Please…


My code for star display

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

[RequireComponent (typeof(Text))]
public class StarDisplay : MonoBehaviour {

public Text starText;
public int stars = 100;
public enum Status {SUCCESS, FAILURE};

// Use this for initialization
void Start () {
	starText = GetComponent <Text> ();
	UpdateDisplay();
}

public void AddStars (int amount) {
	stars += amount;
	UpdateDisplay();
}

public Status UseStars (int amount) {
	if (stars >= amount) {
		stars -= amount;
		UpdateDisplay();
		return Status.SUCCESS;
	}
	return Status.FAILURE;
}

public void UpdateDisplay () {
	starText.text =stars.ToString();
	//Debug.Log("Stars : "+ stars.ToString()) ;
}

}

If that is where you error points to then you have three possible issues;

  • your script doesn’t have a reference to the starText UI Text game object
  • your script doesn’t have a reference to stars
  • or both

Easy way to debug this one would be to comment out this line;

starText.text =stars.ToString();

…and add;

Debug.Log("Stars : ") + stars.ToString();

This would output the number of stars you have to the console, if it errors instead then you know that stars doesn’t exist. If instead it does output the number of stars then you know it must be the reference to the starText UI Text game object that you are missing.

Once you locate the issue you can resolve it, theb delete the suggested line of code above and uncomment your original code so it will work as expected again.

Hope this helps.

Thank you Rob. As your comment I attached Debug line and I got number of stars. As you pointed out, it looks that UI Text game object was missing. However I’m not sure what should I do more, I attached “using.UnityEngine.UI” at first and private Text starText in starDisplay class and starText = GetComponent(); same as Ben’s code for text.

What should I do more for game object, text?

Without seeing your full script it is more difficult but I am assuming starText is defined as a public variable at the top of the script? Assuming so, have you dragged the UI Text object from the Hierarchy in your scene into the Star Text variable exposed in your script?

I did it as you mentioned changing public but… it shows same error.
I guess it was completed delete calling Update() method at Start() method but it didn’t call first number of stars if I would put 0 at Inspector…
Ha… What’s wrong…

Have you dragged the stars Text UI object from the Hierarchy into the “Star Text” variable in the Inspector?

Ofcourse… but It wasn’t help… I did it again right after seen your comment but it didn’t work…
hahahahah…

Can you upload the project in a .zip file to here and I will take a quick look for you, won’t be for about an hour as I have to run my son to school.

it is hard to upload over 10MB… If you don’t mind I can send you an e-mail as attached file…

If you have Google Drive / Dropbox you can just PM me the link if you like?

Jist throwing my 2 pence worth in too.

As @Rob was suggesting i would check to see if
starText is null or not. Just to elimimate.

What i did see is that you are assigning starText to be a public variable of type Text.
So that will allow you to drag into the publicly exposed slot on the inspector ( when you have the object highlighted in the heirarchy that this script is a component of)

Another thing that i noticed is that you are using GetComponent in the start method and essentially overwriting what you would have dragged into the inspector initially.

If you have starText as a public variable and dragged it into the inspector.
Press play, then check the inspector again to see if starText is still populated.

I take it this script is attached to the canvas?
For a Text object added to a canvas, this would be a separate gameobject i would have thought. And as such GetComponent <> only searches for components attached to that specific game object, not a child object of canvas.

If you are able to pop a copy of your project online to have a look at like rob suggested that would be great as we could speculate for a while. And it would be nice to give you a definitive answer so you can move forward :wink:

Wow it was only meant to be a small post, sorry lol)

1 Like

Privacy & Terms