Help With C# Static Boolean!

Hello, I'm a JavaScript user, and I need to change a C# script (its the MouseLook Unity have).

What I need to do is it access a boolean from another script, but it keeps saying '..required to access non static ..'

I need to access the headLock boolean (to make it static), how would I go about doing that:

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {

    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;

    public float minimumX = -360F;
    public float maximumX = 360F;

    public float minimumY = -60F;
    public float maximumY = 60F;

    float rotationY = 0F;
    public bool headLock = false;

    void Update ()
    {
        if (axes == RotationAxes.MouseXAndY && headLock == false)
        {
            float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
        }
        else if (axes == RotationAxes.MouseX && headLock == false)
        {
            transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
        }
        else if (headLock == false)
        {
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
        }

        if(Input.GetButton("Jump")){
            headLock = true;
            print("headLock = true");
        }
        else{
            headLock = false;
            print("headLock = false");
        }

    }

    void Start ()
    {
        // Make the rigid body not change rotation
        if (rigidbody)
            rigidbody.freezeRotation = true;
    }
}

Add the static keyword to the declaration:

public static bool headLock = false;

This allows other scripts to reference the value as follows:

MouseLook.headLock

without having an actual object of type MouseLook. (And no matter how many MouseLook objects exist in your scene, they will all share the same headLock value.)

A few points:

  1. You left out the most important word in that error "an instance is required...". You need to have the script attached to an object then use `GetComponent<>()` to reference it.

  2. Your idea of static doesn't seem to be correct. A static variable is allocated statically. That means it is created when the assembly is loaded. You access it by ClassName.Variable which lets you access it from other classes, but it isn't designed to be treated like an instance variable. More than likely, if you need to access a variable attached to a script on a GameObject then you want to use `GetComponent<>()`. Static variables are for something like Time.deltaTime (ok, it's technically a property) where it isn't specific to any specific instance.