- Home /
Bool wont change... #C
Hi,
I'm starting off making a menu, but I'm instantly running into a problem:
using UnityEngine;
using System.Collections;
public class PauseM : MonoBehaviour {
private bool pausedIO = false;
private bool test = false;
MouseLook mLook;
// Use this for initialization
void Start () {
mLook = (MouseLook)GetComponent(typeof(MouseLook));
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("escape") & !pausedIO)
{
mLook.enabled = false;
pausedIO = true;
}
if(Input.GetKeyDown("escape") & pausedIO)
{
mLook.enabled = true;
pausedIO = false;
}
// Used to see if escape was working or not
if(Input.GetKeyDown("escape"))
{
test = !test;
}
}
void OnGUI(){
GUI.BeginGroup(new Rect(10,10,100,100));
GUILayout.Label("Paused? - " + pausedIO);
GUILayout.Label("test - " + test);
GUI.EndGroup();
}
}
The PausedIO bool will not change and Mouse look is instantly disabled even though it can only do that with an IF on the script.
Also another question, How would I get the mouse Look script from the first person controller?
Answer by robertbu · Mar 11, 2013 at 09:26 PM
Your first 'if' statement sets it to true, but since you don't have an 'else' clause, the next 'if' statement evaluates to true and resets it back to false. You can add an 'else' clause, or restructure the code:
if (Input.GetKeyDown("escape"))
{
if (pausedIO)
{
mLook.enabled = true;
pausedIO = false;
}
else
{
mLook.enabled = false;
pausedIO = true;
}
}
Or even:
if (Input.GetKeyDown("escape"))
{
mLook.enabled = !mLook.enabled;
pauseIO = !pauseIO;
}
Your answer
Follow this Question
Related Questions
Problems with pause menu displaying items and stopping player movement 2 Answers
Menu made of gameobjects C# 2 Answers
Zelda like pause menu 1 Answer
How to make a pause menu in c#? 5 Answers
Creating Pause Game Menu 1 Answer