when esc button pressed, button shows but quickly disappeard from screen
void OnGUI ()
{
if (Input.GetKey (KeyCode.Escape)) {
Time.timeScale = 0.0f;
if (GUI.Button (new Rect (230, 440, 500, 50), "Quit")) {
Application.Quit ();
}
if (GUI.Button (new Rect (230, 495, 500, 50), "No")) {
Time.timeScale = 1.0f;
}
}
}
2 Answers
2
OnGui wont detect you pressing the esc key since its only called only one frame.
Try something like this.
bool pressed = false;
void Update ()
{
if (Input.GetKeyDown ("esc"))
{
pressed = true;
}
}
void OnGUI()
{
if (pressed)
{
Time.timeScale = 0.0f;
if (GUI.Button (new Rect (230, 440, 500, 50), "Quit")) {
Application.Quit ();
}
if (GUI.Button (new Rect (230, 495, 500, 50), "No")) {
Time.timeScale = 1.0f;
}
}
}
}}
GetKey is for when you’re holding down said key, GetKeyDown is when you tap the key. So for this you’d want to do
if(Input.GetKeyDown(Keycode.Escape))
//Instead of
if(Input.GetKey(Keycode.Escape))
i change it , but same ! i should hold the esc key to see the buttons.
– alireza_1395