I’m quite new to Unity, and have been working through the YouTube tutorials for the Unity “roll a ball” game.
Currently i’m up to the stage where you’re counting the objects being collected.
When I run the game, I get the error below, but despite the error, the game runs fine, objects can be picked up and the counter increases;
NullReferenceException: Object reference not set to an instance of an object
PlayerController.SetCountText () (at Assets/scripts/PlayerController.cs:49)
PlayerController.Start () (at Assets/scripts/PlayerController.cs:19)
Attached is a screenshot.
I’ve been through the same section of the tutorial a few times now and cannot for the life of me see where im going wrong.
The only thing I can think of that would be causing this error, would be that the CountText is not set for some reason when the SetCountText function is called.
However, seeing as you are not getting more than one error for this, I assume that this gets fixed latter on.
I suggest that you put a break point on the SetCountText function, and enter Debug mode.
If you don’t know how to do these, just let me know.
The counter increases as it should when you the sphere hits an object, so i’m really confused as to why an error is coming up. Surely if there’s an error with the counter, nothing should happen?
A very common mistake among new Unity users is that they place more than one instance of a script in the scene, and then forget about one of them.
On the top of your hierarchy, there’s a search bar. If you search for “t:PlayerController”, it’ll show you all of the GameObjects in the scene that you have attached player controller to.
I bet you’ve got another, rouge PlayerController there, which hasn’t had the CountText attached.