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
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.
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.
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…
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
Wow it was only meant to be a small post, sorry lol)