Help Understanding Null Reference Exceptions

Code that worked fine just a few minutes ago is now throwing NRE errors for the code that is highlighted.

I understand that an NRE means that I am trying to access something that is not in the scene,
but I do not understand why things are not there when they should be and why I am getting them all the time.
What is it II am not understanding?

1 Like

Morning Dan,

Its generally when we go to try and use a variable that has been declared, but has not been assigned with anything.

So for an example say we have a movement script attached to a player, and that player object had a Rigidbody component attached as well. And we want to apply a force to the player when it starts.

At the top of our script we would declare a reference for the Rigidbody.
Rigidbody myRigidbody;

If in our Start() method we went to try and use this straight away with

myRigidbody.AddForce();

Intellisense wouldn’t complain as you have declared this as a Rigidbody type, and as a Rigidbody type it would have a method AddForce() and intellisense would recognise this.

The issue would come if you went to run it.
It would declare the variable, and then try and call the AddForce() method on it. At that point you would get a null reference exception.

This has to do with the way variables of an object type are declared and used.
When we declared our myRigidbody variable at the top. As a Rigidbody is an object variables of this type are stored as references.
When a variable is declared that refers to an object, if it hasn’t been assigned anything, its value is defaulted to Null.
Thereby a null reference.

So in this example we would declare our myRigidbody, but at this point its null, so we would have to tell it what its meant to be pointing/refering to with.
myRigidbody =GetComponent()

So now we should be ok as we have declared our reference variable, and assigned it before we try and use it.

So one thing I do as a help if I get any errors like this is to check if a reference variable is null before I use it.

For example in your script we could do a little check after walker is assigned just to make sure it found the component.
Just after the assignment
if (walker == Null)
Debug.Log(“walker had not been assigned”)

Sorry its not much, but I’m sitting on a train on my phone, so typings not the best

1 Like

Hello Dan,

I think that the problem may be with the reference of the planeShoot. What is planeShoot? A gameObject? A script? If it is a script, who is his gameobject that he is a component of?

planeShoot is a Class inside Dragon. Dragon is main character in scene and never leaves and never is destroyed.

Am I getting close? The highlighted line gives an error.

1 Like

So, at the moment the line which you have highlighted, the one that should be actually populating planeShoot is commented out. Assuming this hasn’t been done just for this screenshot, the following lines below will return a NullReferenceException because planeShoot = null.

If you uncomment that line, you would then be populating your planeShoot with the result of the GetComponent line above.

Edit

I misread, and can see that Dragon is referenced at the top also. So, have you passed an object into Dragon?

You could either check the inspector, or, if Dragon has some other properties that could be accessed you could print one of them out to the console before the highlighted line to see whether Dragon actually exists at this point. If that then gives you a NRE then you know Dragon is the issue rather than the PlaneShoot component of Dragon.

1 Like

Hey Retro, can you send us a screenshot of the Projectile inspector? planeShoot is the Monobehaviour inside Dragon or it is just a variable/method inside the Dragon’s script? AdjustScore(x) is a public method?

if PlaneShoot is the script, perhaps the problem is happening when you try to access those objects through the inspector, give a try to this code instead the highlighted one (this code don’t depend on setting a reference to the Dragon if the MonoBehaviour planetShoot script exists somewhere in the scene):

planeShoot = FindObjectOfType<PlaneShoot>().GetComponent<PlaneShoot>();
//it will look for a GameObject with a Object named PlaneShoot inside

If it works, then you can pretty much withdraw the public Dragon reference, or try to fix the reference in the inspector.
If it don’t works the problem may be with the script PlaneShoot itself.

What @Rob said would be a good way to know if the problem is with the Dragon Reference too.

1 Like

I commented out that line because it was halting execution.

Dragon is main player who never leaves the scene.
PlaneShoot is the name of his script.
AdjustScore is a function inside PlaneShoot.

1 Like

Strange, dont know what is wrong then. Did you tried the line that I’ve sent to you? Try to exchange it for the one highlighted in your code (there was a typing mistake on the code that I’ve sent to you, it was planetShoot instead of planeShoot).

Try to use planeShoot as private and withdraw the Dragon reference to see if it works

That line worked thank you. I am very discouraged now because the instantiate line in the above has stopped working and is giving me a very similar error: MissingReferenceException even though I have pulled-in Canvas2 into the proper spot in Points. It says Canvas2 does not exists anymore. I don’t understand.

You dont have to feel discouraged, that is normal. The Canvas2 icon inside the projectile’s inspector is strange, try dragging it from the prefab asset again, it should fix it, i dont think that the error is within your coding

I agree with Joao, dont get it get you down. You have came soooo far!

Ive been the same struggling with certain things for ages and not having it stick in my head.

But, once that Eureka moment kicks in, when it comes together it is so worth it!

2 Likes

I have tried everything I can think of and it always says that Canvas2 is null,
I’ve tried creating a whole new prefab and I still get that message.
Is there another way I can get it instead of plugging the prefab into the inspector?
I’ve tried various combination of GetComponent<>()

1 Like

Hey Dan, are you able to zip this up so that we can take a look at the whole thing in Unity…?

1 Like

The canvas has been initialized as Canvas, try to set the reference as public GameObject Canvas2; instead of public Canvas Canvas2;. Since you are istantiating it as a gameobject, it might be causing some problems.
If this dont solve, Would you mind sending another screenshot of how the script is now? Sending the file as Rob said would be a good option too

1 Like

Thankyou so much for the help.

A thunderstorm knocked-out the power.
After the reboot, it works fine. Those darn Gremlins.

3 Likes

Awesome!! Good to know that it is finally working as it should, a hard code to be done you have there! Congratulations Retro!!

1 Like

Wait… That means that your code was powered up by the thunderstorm! How it could get cooler than that?! :joy:

1 Like

Just like Frankenstein.:scream:

2 Likes

lol…

On a positive note, you may be able to sell some bolt-ons :wink:

1 Like