- Home /
Unity 3D C# - Scripts ot true false not working
this is my script
using UnityEngine;
using System.Collections;
public class TheInterface : MonoBehaviour {
//locat objects which controll camera movet
public GameObject CameraMovmentY;
public GameObject CameraMovmentX;
public GameObject BoolBoxSelection;
// Use this for initialization
void Start () {
Screen.showCursor = false;
Screen.lockCursor = true;
CameraMovmentY.gameObject.GetComponent<MouseLook>().enabled = true;
CameraMovmentX.gameObject.GetComponent<MouseLook>().enabled = true;
BoolBoxSelection.gameObject.GetComponent<KillerBoxSelection>().enabled = true;
}
//true or false for if the exscape button is pressed
bool TheExscapePressed = false;
// Update is called once per frame
void Update () {
//if Esc is pressed bool disable and ename curosr movemnt and visability
if (Input.GetKey(KeyCode.Escape))
{
if(TheExscapePressed = false)
{
Screen.showCursor = true;
Screen.lockCursor = false;
CameraMovmentY.gameObject.GetComponent<MouseLook>().enabled = false;
CameraMovmentX.gameObject.GetComponent<MouseLook>().enabled = false;
BoolBoxSelection.gameObject.GetComponent<KillerBoxSelection>().enabled = false;
TheExscapePressed = true;
}
else
{
Screen.showCursor = false;
Screen.lockCursor = true;
CameraMovmentY.gameObject.GetComponent<MouseLook>().enabled = true;
CameraMovmentX.gameObject.GetComponent<MouseLook>().enabled = true;
BoolBoxSelection.gameObject.GetComponent<KillerBoxSelection>().enabled = true;
TheExscapePressed = false;
}
}
}
void OnGUI() {
if(TheExscapePressed == true)
{
//Create a new Button at location 0,0, with a size of 100, 20.
//The text in the button will read "Rotate Once"
if(GUI.Button(new Rect(0,0,100,20), "Exit Game")) {
//if button is pressed, perform the following
Application.Quit();
}
//create another button below "Rotate Once".
//this is a RepeatButton that will continue to perform its action every update
if(GUI.Button(new Rect(0,25,100,20), "Exit To Menu")) {
//if button is pressed, perform the following
}
}
}
}
Forsome reson when i press esc nothing happends, im so confused, when i press esc its meant to show the mouse and the buttons, and when i press esc agian its ment to hide cursor and center it, kinda like call of duty, when in press esc during the game, can any one please help, this code should work, I don't know!
Answer by robertbu · Apr 27, 2014 at 06:21 AM
Your problem is on this line:
if(TheExscapePressed = false)
You are assigning the value, not comparing the value. The code should be:
if(TheExscapePressed == false)
Or better yet:
if(!TheExscapePressed)
You fixed that problem thank you, but another problem has came up, it works like 2 times, sometimes I need to press esc, like 5 more times to get back to the pause menu again.
Ins$$anonymous$$d of using Input.Get$$anonymous$$ey() try using Input.Get$$anonymous$$eyDown().
Answer by Stupid Bird · Apr 27, 2014 at 08:02 AM
I think you should more careful when you edit your code and kown more about what method you will use..
Your answer
Follow this Question
Related Questions
input question 1 Answer
Script that makes the mouse cursor invisible. 3 Answers
Escape menu script problem 1 Answer
How do you make a Main Menu that only takes Joystick input and is not affected by the Cursor 2 Answers
Cursor.visible issues 0 Answers