- Home /
Pause menu make unity stuck
Sometimes the pause menu stuck unity and sometimes not, this is my script:
using UnityEngine;
using System.Collections;
public class Pause : MonoBehaviour {
bool pressedEscape = true;
void Start () {
Screen.showCursor = false;
}
void Update () {
if(Input.GetKeyUp(KeyCode.Escape) && !pressedEscape){
Screen.showCursor = false;
pressedEscape = true;
Time.timeScale = 1;
GameObject.Find("First Person Controller").GetComponent<MouseLook>().enabled = true;
}else if(Input.GetKeyUp(KeyCode.Escape) && pressedEscape){
pressedEscape = false;
Screen.showCursor = true;
Time.timeScale = 0;
GameObject.Find("First Person Controller").GetComponent<MouseLook>().enabled = false;
}
}
void OnGUI(){
if(!pressedEscape){
if(GUI.Button(new Rect(300, 300, 200, 50), "Attachments")){
}
}
}
}
Whats the problem with this script?, thanks in advance
explain stuck...Does your cursor show up/and or can you move your camera?
By saying stuck i mean the program is not responding, and i dont know how to explain it but its like a boolean when starting the game the "boolean" setting to true or false and by this "boolean" the game getting stuck/not getting stuck (there isn't really boolean its just an example)
Answer by SubatomicHero · Jul 16, 2013 at 09:54 AM
To make this a little simpler to read see if these changes help:
public class Pause : MonoBehaviour {
bool pressedEscape = false; // ..because we havent pressed it yet
void Start(){
Screen.showCursor = false;
}
void Update(){
if (Input.GetKeyUp(KeyCode.Escape){
pressedEscape = !pressedEscape;
// if it is true its now false
// if it is false its now true;
}
// we then check the value of pressedEscape
if (pressedEscape){ // pressedEscape is true so pause
changePauseStatus(true, 0, false);
} else { // pressedEscape must be false
changePauseStatus(false, 1, true);
}
}
void changePauseStatus(bool showCursor, int timeScale, bool fpsEnabled){
Screen.showCursor = showCursor;
Time.timeScale = timeScale;
GameObject.Find("First Person Controller").GetComponent<MouseLook>().enabled = fpsEnabled;
}
If you still have crashing issues I suspect its the GameObject.Find lines not finding the gameobject.....maybe. Sometimes simplifying your code can iron out small bugs in it :D
I've already fixed it, if some one want to know how: its look like changing the zero in the code to 0.00001f make it not crash.
Please update your original question with the fixed code if you resolved it. This will help people in the future.
Your answer
Follow this Question
Related Questions
How to export MMD model to Unity? 0 Answers
A node in a childnode? 1 Answer
Object moves out of own collider. 3 Answers
Very little, but very heavy script problem. 0 Answers
Blender Animation Problems 3 Answers