Null Reference Exception

Sorry, didn’t know how to use GoogleDrive hahaha

https://drive.google.com/drive/folders/10x-oBDWlURUV-K1foDoyV7XjS6fCy9ro?usp=sharing

Hi,

I waited as long as I could but had to get some sleep in the end, grabbing a copy of your project now, will take a look :slight_smile:


Updated Wed Sep 05 2018 13:53

Just open up the project, I wasn’t sure which scene specifically you were running, so I started with Combat Sandbox, no errors. I placed the mouse over an enemy and it changed to a sword icon, I moved the mouse over the grass, it changed to an arrow - seems ok.

I then tried the Level 1 Village scene, and did encounter an error, in fact, I saw three errors;

image

Starting from the top, the first error is caused because this line of code;

cameraRaycaster.notifyMouseClickObservers += ProcessMouseClick;

is trying to act upon members of a variable, cameraRaycaster, which itself is null. The reason cameraRaycaster is null is due to the previous line in the Awake statement;

cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();

Here, you are using GetComponent and specifying the type as CameraRaycaster, but if you look at your Main Camera you’ll note that you don’t have a CameraRaycaster component attach to it, what you do have is a CameraRaycaster_Old component attached. It looks like you have probably renamed it but not changed this line above, and as such the variable is null because no component was found/returned.

The second error message is due to the exact same problem, again, no CameraRaycaster component could be found/was returned, due to the renaming.

And finally, the third error is caused because the delegate has no subscribers, because of the renaming, and the cameraRaycaster variable being null in the other two scripts, nothing actually ever subscribed.

So, to start to resolve this problem I would suggest you either remove the component flagged old if it shouldn’t be attached anymore, and then attach the correct one - or - rename this one accordingly so that your code can find the correct component.

After this, let me know where things are :slight_smile:


Updated Wed Sep 05 2018 14:47

If you add the CameraRaycaster.cs script component, and remove the CameraRaycaster_OLD.cs script component and then run the game you’ll be presented with the following error;

image

If we look at the code in question;

if (EventSystem.current.IsPointerOverGameObject ())
{
    NotifyObserersIfLayerChanged (5);
    return; // Stop looking for other objects
}

Line 54 is the first line, you are trying to acces the EventSystem, if you check your scene you’ll note that you don’t have one of these objects in the Hierarchy.

After adding one, and running the scene again, no errors are presented, instead your Debug.Log messages appear in the console;

image

The next issue is that you can’t really test the cursor affordance because of the position of your player in the scene, so, if you move your player, for now, to somewhere like 5, -100, 210, and then drag an enemy prefab into the scene, somewhere near to the player and run the game, you can now move your mouse over all of the layers you are interested in, the ground, the enemy and the unknown grey world space…


(video best played in full screen mode for clarity)

Okay! Sorry for the long wait! My college classes started ahahah
I’m looking at everything now, and Wow… Thank you. To be honest, I think I got all mixed up with version control at some point. I must have had trouble, reverted to an older build thinking my scripts had been added at that point, and they weren’t and dug myself a deeper trench…

I am going to be So much more careful in checking that all proper scripts are attached for now on.

Now, I am getting “Click” responses when I expect them in both scenes.

However, I am getting similar NullReferenceExceptions when moving my cursor from a walkable layer to an enemy layer. So, anytime I change layer, I get this null reference exception. This time the NullReference is in regard to “notifyLayerChangeObservers”.

I then did as you recommended above:

if (notifyLayerChangeObservers != null)
            {
                notifyLayerChangeObservers (newLayer);
            }

since “It’s typically a bad idea to call the delegate without checking first that it isn’t null, e.g. whether it has any subscribers.” On that change, SUCCESS! Everything works as it should!

But, I am still confused as to why this happens the way it does.

So here is where my understanding of delegates falls flat. I am wondering what the notifyLayerChangeObservers is read as at different times and why it does so.

I changed the code to:

void NotifyObserersIfLayerChanged(int newLayer)
	{
		if (newLayer != topPriorityLayerLastFrame)
		{
			topPriorityLayerLastFrame = newLayer;

            Debug.Log(notifyLayerChangeObservers);
            notifyLayerChangeObservers (newLayer);
		}
	}

On play, the console reads out:

1.)  CameraRaycaster+OnCursorLayerChange
UnityEngine.Debug:Log(Object)

2.) 9
UnityEngine.Debug:Log(Object)
CameraRaycaster:NotifyObserersIfLayerChanged(Int32) (at Assets/Camera and UI/CameraRaycaster.cs:97)
CameraRaycaster:Update() (at Assets/Camera and UI/CameraRaycaster.cs:68)

3.) Null
UnityEngine.Debug:Log(Object)
CameraRaycaster:NotifyObserersIfLayerChanged(Int32) (at Assets/Camera and UI/CameraRaycaster.cs:96)
CameraRaycaster:Update() (at Assets/Camera and UI/CameraRaycaster.cs:74)

4.)  9
UnityEngine.Debug:Log(Object)
CameraRaycaster:NotifyObserersIfLayerChanged(Int32) (at Assets/Camera and UI/CameraRaycaster.cs:97)
CameraRaycaster:Update() (at Assets/Camera and UI/CameraRaycaster.cs:68)

5.)  NullReferenceException: Object reference not set to an instance of an object
CameraRaycaster.NotifyObserersIfLayerChanged (Int32 newLayer) (at Assets/Camera and UI/CameraRaycaster.cs:97)
CameraRaycaster.Update () (at Assets/Camera and UI/CameraRaycaster.cs:74)

6.)  CameraRaycaster+OnCursorLayerChange
UnityEngine.Debug:Log(Object)
CameraRaycaster:NotifyObserersIfLayerChanged(Int32) (at Assets/Camera and UI/CameraRaycaster.cs:96)
CameraRaycaster:Update() (at Assets/Camera and UI/CameraRaycaster.cs:74)

7.)  5
UnityEngine.Debug:Log(Object)
CameraRaycaster:NotifyObserersIfLayerChanged(Int32) (at Assets/Camera and UI/CameraRaycaster.cs:97)
CameraRaycaster:Update() (at Assets/Camera and UI/CameraRaycaster.cs:57)

8.)  Null
UnityEngine.Debug:Log(Object)
CameraRaycaster:NotifyObserersIfLayerChanged(Int32) (at Assets/Camera and UI/CameraRaycaster.cs:96)
CameraRaycaster:Update() (at Assets/Camera and UI/CameraRaycaster.cs:74)

9.)  5
UnityEngine.Debug:Log(Object)
CameraRaycaster:NotifyObserersIfLayerChanged(Int32) (at Assets/Camera and UI/CameraRaycaster.cs:97)
CameraRaycaster:Update() (at Assets/Camera and UI/CameraRaycaster.cs:57)

10.)  NullReferenceException: Object reference not set to an instance of an object
CameraRaycaster.NotifyObserersIfLayerChanged (Int32 newLayer) (at Assets/Camera and UI/CameraRaycaster.cs:97)
CameraRaycaster.Update () (at Assets/Camera and UI/CameraRaycaster.cs:74)

On start of the game, the cursor is on a walkable layer.

So, on print-out 1 (PO1) the game checks that specific delegate (OnCursorLayerChange), and finds that the layer is valued at 9 (PO2).

At PO3, it reads Null. Why is the delegate reading null if we subscribed OnLayerChange to cameraRaycaster.notifyLayerChangeObservers on Awake? Or is the delegate only NOT null when notifying the observers, then switches back to null immediately after?

PO5 is the error that follows immediately after.

I move my cursor from a walkable layer to an enemy layer and the same thing happens over.

Just to be clear, All Issues Are Solved! I am just hoping that myself and all future confused readers can gain some insight into how this is functioning to cause errors.

Thanks!

Hi,

I’m looking at everything now, and Wow… Thank you.

You are very welcome :slight_smile:

On that change, SUCCESS! Everything works as it should!

Great! :slight_smile:

But, I am still confused as to why this happens the way it does.

I’m fairly certain this comes down to the ordering of your event functions.

I don’t have a copy of your project at this time, but I’m fairly certain not all of delegates were being set up in the Awake methods, and, unless you are using script ordering, you cannot be certain of the execution order of the scripts, e.g. which of their Start methods will be called first. My belief is that you are effectively trying to access something which hasn’t been set up yet.

With regards to your printouts ordering above, are you saying that since making the changes above you are still receiving errors?

It’s obviously hard for me to diagnose from this end with only that. The video I took was of the scene mentioned after correcting the issue and no further errors were displayed. I moved the mouse from walkable, unknown, enemy etc, no errors were displayed.

Happy to take another look if you want to share the entire project files again, including any changes you have made).

Privacy & Terms