Inacessible due to protection level

I changed the naming of variables in my code to the same as in the code directly from the course and now I get a few errors. both are cameraRaycaster.layerHit is inacessible due to it’s protection level (39,52) and (40,29). Here is the script:

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.Characters.ThirdPerson;

[RequireComponent(typeof (ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
{

	[SerializeField] float walkMoveStopRadius = 0.25f;

    ThirdPersonCharacter Character;   // A reference to the ThirdPersonCharacter on the object
    CameraRaycaster cameraRaycaster;
    Vector3 currentClickTarget;

	bool isInDirectMode = false; 
        
    private void Start()
    {
        cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
        Character = GetComponent<ThirdPersonCharacter>();
        currentClickTarget = transform.position;
    }

    // Fixed update is called in sync with physics
    private void FixedUpdate()
	{

		if (Input.GetKeyDown(KeyCode.G)) { 
			isInDirectMode = !isInDirectMode; 
			currentClickTarget = transform.position; //clears target
		}

		if (isInDirectMode) {
			ProcessDirectMovement ();
		} else {
			if (Input.GetMouseButton (0)) {
				print ("Cursor raycast hit " + cameraRaycaster.layerHit);
				switch (cameraRaycaster.layerHit) {
				case Layer.Walkable:
					currentClickTarget = cameraRaycaster.hit.point; 
					break;
				case Layer.Enemy:
					print ("Not moving to enemy");
					break;
				default:
					print ("SHOULDN'T BE HERE");
					return;
				}

			}
			var playerToClickPoint = currentClickTarget - transform.position;
			if (playerToClickPoint.magnitude >= walkMoveStopRadius) {
				Character.Move (currentClickTarget - transform.position, false, false);
			} else {
				Character.Move (Vector3.zero, false, false);
			}

		}
	}

	private void ProcessDirectMovement() {

		print ("Direct Movement"); 

		float h = Input.GetAxis("Horizontal");
		float v = Input.GetAxis("Vertical");

		Vector3 CamForward = Vector3.Scale (Camera.main.transform.forward, new Vector3 (1, 0, 1)).normalized;
		Vector3 Move = v * CamForward + h * Camera.main.transform.right;

		Character.Move (Move, false, false);
	}

	//TODO Fix Nullreference exception reference not set to instance of object
	// line 45 playermovement.fixedupdate.
}

Hi @Brocolli,

In your CameraRaycaster script, what have you set the scope to of layerHit?


See also;

I am not sure what is meant by the scope of layerhit is. Here is my raycaster script, it should be the same as the completed version from the course:
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 delegate type
public event OnLayerChange onLayerChange; // Instantiate observer set

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

void Start()
{
    viewCamera = Camera.main;
	//onLayerChange(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)
        {
			raycastHit = hit.Value;
			if (layerHit != layer) { //if layer changed
				layerHit = layer;
				onLayerChange(layer); //call delegates
			}
			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;
}

//understand getter (get; set)
//understand delegate

}

Hi,

scope as in private, public etc.

If you look at your code you have layerHit as a private variable.

You are returning this via a public property called currentlayerHit, yet in your other script (first post) you are trying to access the private variable layerHit, hence the error.

Thanks now the program will run.

1 Like

No worries, glad you cam move forward again :slight_smile:

Privacy & Terms