Problem with Delegating; Object reference not set to an instance of an object

rpg_s01_using_delegates_in_c

#1

I am simply just trying to get delegates to work, however I am getting a NullReferenceExcpetion and I’m not sure why. I’m using visual studio 2017 for Mac, not sure if that affects anything.

CameraRaycaster:

    public delegate void OnLayerChange();  
    public OnLayerChange layerChangeObservers; 

    void Start() 
    {
        viewCamera = Camera.main;
    }

    void Update()
    {
        foreach (Layer layer in layerPriorities)
        {
            var hit = RaycastForLayer(layer);
            if (hit.HasValue)
            {
                raycastHit = hit.Value;
                if(layerHit != layer)
                {
                    layerHit = layer;
                    layerChangeObservers(); // this is where the error is occurring
                }
                layerHit = layer;
                return;
            }
        }

        // Otherwise return background hit
        raycastHit.distance = distanceToBackground;
        layerHit = Layer.RaycastEndStop;
    }

CursorAffordance:

    private void Start()
    {
        cameraRaycaster = GetComponent<CameraRaycaster>();
        cameraRaycaster.layerChangeObservers += OnLayerChanged;
    }

    void OnLayerChanged () {
        print("Changed");
        switch (cameraRaycaster.currentLayerHit){
            case Layer.Walkable:
                Cursor.SetCursor(walkCursor, cursorHotspot, CursorMode.Auto);
                break;
            case Layer.Enemy:
                Cursor.SetCursor(targetCursor, cursorHotspot, CursorMode.Auto);
                break;
            case Layer.RaycastEndStop:
                Cursor.SetCursor(unknownCursor, cursorHotspot, CursorMode.Auto);
                break;
        }
	}

#2

I can’t see an error… can you copy and paste the whole error message, so we can see the bug trace, line numbers and positions of bug detection.?

I do have one idea though: try changing that line to:

this.playerChangeObservers(); // this is where the error is occurring

That ‘this’ keyword will ensure that the delegate is called as if like a method of the actual instance of CameraRaycaster.

Your syntax, without the ‘this’, looks more like static methods to me. Personally, I prefer static methods. If my ‘this’ idea above don’t work, try changing your second line to:

public static event OnLayerChange layerChangeObservers;

But don’t use both the ‘this’ keyword and the ‘static’ keyword. They work in opposite ways!! Although I think they can be both used to the same effect. As I said, I prefer static methods and try to avoid ever using the ‘this’ keyword, but your programming approach my differ, and that’s good!


#4

Can’t initially see a problem here, but very hard without the error code output and a line, can you send us that? This version of update from my code is working fine, I can’t see any obvious differences

void Update()
{
    // Look for and return priority layer hit

    // look through all the layers in the layer priority list
    foreach (Layer layer in layerPriorities)
    {
        // raycast for the layer
        var hit = RaycastForLayer(layer);

        // if it finds something
        if (hit.HasValue)
        {
            // check if it is different to the current layer
            if (currentLayerHit != layer)
            {
                // if it is, update the value of currentLayerHit to new value
                raycastHit = hit.Value;
                currentLayerHit = layer;

                // inform observers that layer has changed 
                layerChangeObservers();
                
            }
            return;
        }
    }