NullReferenceExeption after adding delegates to CameraRaycaster

NullReferenceException: Object reference not set to an instance of an object
CameraRaycaster.Update () (at Assets/Camera & UI/Scripts/CameraRaycaster.cs:46)

CameraRaycaster.cs:

using UnityEngine;

public class CameraRaycaster : MonoBehaviour {
public Layer layerPriorities = {
Layer.Enemy,
Layer.Walkable
};

[SerializeField] float distanceToBackground = 100f;
Camera viewCamera;

RaycastHit raycastHit;
public RaycastHit hit
{
    get { return raycastHit; }
}

Layer layerHit;
public Layer currentLayerHit
{
    get { return layerHit; }
}

public delegate void OnLayerChange(Layer newLayer); // declare new delegate type
public event OnLayerChange onLayerChange; // Instantiate an observer set

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

void Update()
{
    // Look for and return priority layer hit
    foreach (Layer layer in layerPriorities)
    {
        var hit = RaycastForLayer(layer);
        if (hit.HasValue)
        {
            raycastHit = hit.Value;

            if(layerHit != layer)
            {
                layerHit = layer;
                onLayerChange(layer);  // This is the line that Unity thinks is the issue
            }
            layerHit = layer;
            return;
        }
    }

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

RaycastHit? RaycastForLayer(Layer layer)
{
    int layerMask = 1 << (int)layer; // See Unity docs for mask formation
    Ray ray = viewCamera.ScreenPointToRay(Input.mousePosition);

    RaycastHit hit; // used as an out parameter
    bool hasHit = Physics.Raycast(ray, out hit, distanceToBackground, layerMask);
    if (hasHit)
    {
        return hit;
    }
    return null;
}

}

CursorAffordance.cs

using UnityEngine;

[RequireComponent( typeof(CameraRaycaster))]
public class CursorAffordance : MonoBehaviour {

[SerializeField] private Texture2D walkCursor = null;
[SerializeField] private Texture2D targetCursor = null;
[SerializeField] private Texture2D unknownCursor = null;

[SerializeField] private Vector2 cursorHotspot = new Vector2(0, 0);
private CameraRaycaster cameraRaycaster;

// Use this for initialization
void Start () {
cameraRaycaster = GetComponent();
cameraRaycaster.onLayerChange += OnLayerChanged;
}

void OnLayerChanged(Layer newLayer) {
switch(newLayer)
{
case Layer.Enemy:
Cursor.SetCursor(targetCursor, cursorHotspot, CursorMode.Auto);
break;

        case Layer.Walkable:
            Cursor.SetCursor(walkCursor, cursorHotspot, CursorMode.Auto);
            break;

        case Layer.RaycastEndStop:
            Cursor.SetCursor(unknownCursor, cursorHotspot, CursorMode.Auto);
            break;

        default:
            Debug.LogError("Don't know what cursor!");
            return;
    }

}
// TODO consider de-registration for delegate: OnLayerChanged() when leaving to game scenes.
}

NOTE: Not sure why this isn’t formatting (indents) correctly. I commented the line in CameraRaycaster.cs that Unity is complaining about.

I’ve watched the videos a couple times and don’t see where I made the mistake. My game still runs but annoying. I am using Unity 2018.2.16f1

Found the issue. Apparently my Camera AND camera arm had a CameraRaycaster component attached. i removed the CameraRaycaster component from the cameraArm.

How did Ben put it, in an early video? “Rookie mistake”

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms