Error In Roll A Ball

Hi there

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.

Can anyone help?

Thanks

When you double click on the error, what line does it take you to in your script?

It goes to line 48

2651584--186843--Capture.PNG

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. :slight_smile:

You probably missed a drag and drop step in the inspector. Does CountText have a value in the inspector?

If you could give me some pointers, that would be helpful, thanks

I’m pretty sure i’ve done it right, hopefully the attached shows this.

2652154--186871--Capture.PNG

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?

Any ideas?

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.

1 Like

I think you mean rogue. If coloring scripts was something you could do that would be awesome! :stuck_out_tongue:

@markuk2016 Try putting:

Debug.Log(gameObject.name);

Right after the code in the error, this will tell you what GameObject’s PlayerController script is giving this error.

2 Likes

Thanks all, I had the player controller script attached to the main camera. No errors now :slight_smile:

2 Likes