error CS0120: An object reference is required for the non-static field, method, or property when trying to reference a bool?

So I’m trying to reference a bool from one of my scripts to another in order to make sure that the player can’t switch lanes when the invisible game object is colliding with something and I keep getting this error on my 2nd if statement when I attempt to reference the bool.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    // variables
    public PathSwitcher pathSwitcher;
    private Rigidbody playerRb;
    public float horizontalInput;
    public float movementSpeed = 8.0f;
    public float jumpForce = 7.0f;
    public bool isOnGround = true;
    public float pathOne = -1.0f;
    public float pathTwo = 1.25f;
    public GameObject pathSwitcherTwo;

    // Start is called before the first frame update
    void Start()
    {
        // Getting the component for the player rigidbody
        playerRb = GetComponent<Rigidbody>();
        pathSwitcherTwo = GameObject.Find("Path2CollisionDetector");
        pathSwitcher = pathSwitcherTwo.GetComponent<PathSwitcher>();

    }

    // Update is called once per frame
    void Update()
    {
        // code that recieves horizontal inputs and moves the player accordingly
        horizontalInput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * movementSpeed);

        // if statement to make the player jump when input is recieved
        if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
        {
            playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isOnGround = false;
        }

        if (Input.GetKeyDown(KeyCode.UpArrow) && PathSwitcher.pathTwoClear = true)
        {
            playerRb.velocity = new Vector3(0, 0, 5);
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            playerRb.velocity = new Vector3(0, 0, -5);
        }

        if(transform.position.z < pathOne)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, pathOne); 
        }

        if(transform.position.z > pathTwo)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, pathTwo);
        }

    }

    // a function which deals with all of the for the player
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isOnGround = true;
        }

        if (collision.gameObject.CompareTag("Path_1_Obstacle"))
        {
            playerRb.velocity = new Vector3(0, 0, 5);
        }

        // if (collision.gameObject.CompareTag("Path_2_Obstacle"))
        {
           // playerRb.velocity = new Vector3(0, 0, -5);
        }
    }
}

Below is the script that I am trying to reference the bool from.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PathSwitcher : MonoBehaviour
{
    public bool pathTwoClear = true;
    private Rigidbody pathTwoRb;
    // Start is called before the first frame update
    void Start()
    {

        pathTwoRb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void PathTwoClear(Collision collision)
    {
        if (collision.gameObject.CompareTag("Path_2_Obstacle"))
        {
            pathTwoClear = false;
        }
    }
}

Hi, you’ve got to use the stored path switcher that you declared earlier:

public PathSwitcher pathSwitcher;

And whatever name you gave it, (in this case “pathSwitcher”,) is the one you should use when you want to access it’s variables, so switch PathSwitcher.pathTwoClear
to pathSwitcher.pathTwoClear


The compiler is confused because you’re using the syntax of static variables, such as Vector2.up or Quaternion.identity as opposed to instance variables like rb.velocity.

Hope this helps!