NullReferenceExpection

I am a high school student and my high school offers a course in unity but its self directed. So there is little help…
Im getting an error

NullReferenceException: Object reference not set to an instance of an object
Gun.CanShoot () (at Assets/Scripts/Gun.cs:107)
Gun.Shoot () (at Assets/Scripts/Gun.cs:49)
Gun.ShootContinuous () (at Assets/Scripts/Gun.cs:83)
PlayerController.Update () (at Assets/Scripts/PlayerController.cs:51)

and the 2 scripts you need
http://pastebin.com/bVjPVEqF
http://pastebin.com/ezgwPh77

Please this would help alot!

Part of self-direction is asking for help, so you’ve got it, and I’m sure someone is sifting through the code right now with their spare time, probably waiting for answers themselves… meanwhile though I can give you some helps for other ways to learn on your own.

First thing is the error, this is a common error, it’s a “Null Reference Error”, meaning something being referred to is null, it has no entry, and a line is missing to activate or fill it.

Next is the debug log, it’s breaking it down to four lines this error is running through, all of which are connected. I’m guessing something like Update has Shoot, which contains in some order, CanShoot and ShootContinuous? Look at each of the lines in the debug log and figure out what the top level is being caught with an error. Probably CanShoot or ShootContinuous on lines 107 and 83 respectively, there is a variable that is empty, it has nothing in it when the code gets there.

This could be for a couple reasons:

  1. You haven’t “initiated” a variable you’re now filling. For instance, if I make a private Vector3, I can’t use it until I assign a “new” Vector3, or an already existing Vector3. So one of these lines could be missing a “new” keyword, or one of the variables might need to be initiated earlier in the code, so the assignment has somewhere to go.

  2. You’ve assigned something that is also null, or doesn’t exist. So double check that the variable you’re filling it with is also a valid variable at that point.

I hope this helps, if not someone else will surely come along eventually. Helping each other is how we make things move forward faster, the world would crawl if it didn’t happen constantly, good luck!

Change this line

RaycastHit hit;

To

RaycastHit hit=new RaycastHit();

You need to set the raycasthit to an actual object, but you’re just declaring the variable.
Null references are one of the easiest things to fix. The error tells you exactly where the problem is.

You haven’t initialized the playercontroll variable anywhere in your code, so it’s going to be null.

If you don’t know why you need to initialize reference variables or how to do it, then you’re probably need to spend some time reading up on C# and programming.

“new” Keyword :stuck_out_tongue:

Debugging will teach you a lot about your syntax, just keep at it. Google the error as well when you haven’t encountered it before, most errors you’ll receive early on are common and can be explained either through explanation or demonstration, fixing them will help you understand the importance of, impact of, and proper use of important concepts like declaring, initiating, modifying and converting variables.

That’s right in the general case but is incorrect in this particular scenario since PlayerController is a MonoBehaviour.

You should go read how Physics.Raycast works :slight_smile:

Ouch. Or I should just not try to wake up and start right on answering questions on my phone.

Well since I haven’t seen any help that I havent tried. The entire project zipped up into a nice file Microsoft OneDrive

playerControll is null which means you’ve never given it a value. That’s the bug.