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;
}
}