Boolean not functioning as intended.

Okay so I’m working on a simple first person game and I’m currently in the process of making a start menu for the game.

I’ve made the menu and everything, but the problem comes when my “Play”-button is supposed to make the player movements available by making the “running” boolean that I made “true”.

Here’s the code for the player controller:

public class FirstPersonController : MonoBehaviour {

    public float movementSpeed = 5;
    public float rotateSpeed = 3;

    public bool running = false;

    float rotZ = 0;

    public float zRange = 60;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

        if (running == true)
        {
            //Mouse
            Cursor.lockState = CursorLockMode.Locked;
            //Cursor.visible = false;


            //Rotation
            float rotX = Input.GetAxis("Mouse X") * rotateSpeed;

            rotZ -= Input.GetAxis("Mouse Y") * rotateSpeed;
            rotZ = Mathf.Clamp(rotZ, -zRange, zRange);
            Camera.main.transform.localRotation = Quaternion.Euler(rotZ, 0, 0);

            transform.Rotate(0, rotX, 0);

            //Movement
            float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
            float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;

            Vector3 speed = new Vector3(sideSpeed, 0, forwardSpeed);

            speed = transform.rotation * speed;

            CharacterController cc = GetComponent<CharacterController>();

            cc.SimpleMove(speed);
        }


	}
}

And here’s the code for the menu:

public class menuScript : MonoBehaviour {

    public Canvas quitMenu;
    public Button startText;
    public Button exitText;

    FirstPersonController fpcScript;

    GameObject fpcGameObject;

	// Use this for initialization
	void Start () {

        quitMenu = quitMenu.GetComponent<Canvas>();
        startText = startText.GetComponent<Button>();
        exitText = exitText.GetComponent<Button>();
        fpcScript = fpcGameObject.GetComponent<FirstPersonController>();

        quitMenu.enabled = false;

	}
	
	public void ExitPress(){

        quitMenu.enabled = true;
        startText.enabled = false;
        exitText.enabled = false;

    }

    public void NoPress(){

        quitMenu.enabled = false;
        startText.enabled = true;
        exitText.enabled = true;

    }

    public void StartLevel(){

        fpcScript.running = true;

    }

    public void ExitGame(){

        Application.Quit();

    }

}

Would greatly appreciate some help with this. I’ve been stuck with this for a few hours now and I just can’t get it to work.

Both of these fields are private, so they cant be set from inspector.

FirstPersonController fpcScript; 
GameObject fpcGameObject;

In start you try to get the FirstPersonController from an uninitialized reference.

fpcScript = fpcGameObject.GetComponent<FirstPersonController>();

This will fail and you should see an error in the console. As a result, fpcScript will remain null.

Then you are trying to set the running boolean on nothing.

public void StartLevel() {
    fpcScript.running = true; // Note! fpcScript is null! 
}

So the problem isn’t with your boolean. The problem is that you don’t have a reference to the controller.

Fix it by providing a reference to the game object that has the script.

public GameObject fpcGameObject; // now it's public, set it up in the inspector.