Layer Errors CameraRaycaster.cs

Want to go further in the course but I am stuck because of a few errors. As the code is here it says unexpected symbol “newLayer” (33,29).

using UnityEngine;

public class CameraRaycaster : MonoBehaviour
public Layer[] layerPriorities = {

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

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

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

public delegate void OnLayerChange(Layer newLayer); // Declare delegate type
public event OnLayerChange layerChangeObservers; // Instantiate observer set

//Event protects onlayerchange from being overwritten (can only be += or -=

void Start()
    viewCamera = Camera.main;
	layerChangeObservers(Layer newLayer); // Call delegates

void Update()
    // Look for and return priority layer hit
    foreach (Layer layer in layerPriorities)
        var hit = RaycastForLayer(layer);
        if (hit.HasValue)
            m_hit = hit.Value;
			if (layerHit != layer) { //if layer changed
				layerHit = layer;
				layerChangeObservers(layer); //call delegates
			m_layerHit = layer;

    // Otherwise return background hit
    m_hit.distance = distanceToBackground;
    m_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;


If I remove what is in the brackets (Layer newLayer) it says that it doesn’t take 0 arguments and another error comes up saying layerhit can’t be assigned to it is read only. I do not understand I followed the course exactly

In the brackets you just need to pass the variable, not the data type. So just pass newLayer in the brackets, not Layer newLayer.

Hope this helps :slight_smile:

Privacy & Terms