gui button disapears quickly!

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;
			}
		}
	}

i change it , but same ! i should hold the esc key to see the buttons.

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;
             }
}
         }
     }}

This or then MovePosition that works pretty much like Translate, but doesn't ignore collisions.

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))

This won't help either as you would only get the buttons in the frame the user pressed ESC. He should add that if in the Update() method and toggle a boolean when the user presses ESC (Input.GetKeyDown()), because the button has to be drawn repeatedly in the OnGui() method as long as the menu should be visible (most usually: first ESC button press = show, second one (or the "No" button) = hide again).

Ah, alright. My apologies, I'm not too familiar with the legacy gui system.