Chapter 4 Ray Casting Not Working

I can’t get my bobblehead character to follow my mouse position. The final project I downloaded seems to work fine, but my version doesn’t. I keep getting the following error:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.cs:68). The code at line 68 is Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);. I would greatly appreciate any assistance!

1 Like


Sorry to hear you are running into problems. That can frustrating, especially if you are making progress. Without seeing your project, I can only assume that the null pointer is coming from the camera.

Select your camera in the hierarchy, and check to see if it has the main camera tag assigned to it. That’s the first place I’d look.

Give it a shot and let me know how it turns out. Cheers!


Worked perfectly! Thanks!

That’s great to hear! Enjoy the rest of the book!

1 Like

I had the same problem, it was driving me nuts hahaha, thanks @bdmoakley

I too was having the same problem, i guess it should have a little tip box for this trouble shooting, because its not very clear what it’s wrong, i thought it was the script problem, but never would have guess it was a very simpler solution. :slight_smile:

thanks @bdmoakley once again!

Thanks that fixed it. I was wondering if it was because I was using a Mac and a Trackpad. Using any Apple product is slightly problematic because you pretty much only have a left mouse button, so navigating the Scene is a pain in the Ass :stuck_out_tongue:

Absolutely - 3d programs like Unity and Blender are best used with a 3 button mouse. But this seems like a common problem. I’ll make sure to add a note in the next edition.

This topic was automatically closed after 166 days. New replies are no longer allowed.