Make A Pause Menu With Buttons (JavaScript)

So far I have this and I want to make it so when I press “Resume” or “Unpause” it resumes or unpauses the game… I basically want to know how to use goto OnGUI or something… I’ve tried many diffrerent ways. goto function OnGUI - goto OnGUI - get function OnGUI - get OnGUI, etc.

#pragma strict

function Update(){
	if(Input.GetKeyDown("e")){
	Time.timeScale = 0;
	get OnGUI;
	}
}

function OnGUI () {
    // Make a background box
    GUI.Box (Rect (10,10,100,90), "Loader Menu");

    // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
    if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
        Application.LoadLevel (1);
    }

    // Make the second button.
    if (GUI.Button (Rect (20,70,80,20), "Level 2")) {
        Application.LoadLevel (2);
    }
}

3 Answers

3

+MartinTV +zharik86 I thank both of you for an answer, sorry for late response, shortly after asking this question my internet was cut off due to high winds (Arizonians don’t prepare for high winds :slight_smile: ) but zharik86’s works better. No offense to you MartinTV. I will thumbs up for both of you and vote zharik86 better. (Yes I know I put this in the comments, it’s my question, FTW! :D)

If you want use pause/resume action for your game, than create one more variable. Code see below:

 #pragma strict

 private var paused: boolean = false; //variable for detect state game pause/unpause

 function Update() {
  //Best use KeyCode
  if(Input.GetKeyDown(KeyCode.E)) {
   if(paused) {
    Time.timeScale = 1;
    paused = false;
   } else {
    Time.timeScale = 0;
    paused = true;
   }
  }
 }

 function OnGUI () {
  //All elements for unpaused game? Create condition
  if(!paused) {
   // Make a background box
   GUI.Box (Rect (10,10,100,90), "Loader Menu");

   // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
   if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
    Application.LoadLevel (1);
   }

   // Make the second button.
   if (GUI.Button (Rect (20,70,80,20), "Level 2")) {
    Application.LoadLevel (2);
   }
  }
 }

I hope that it will help you.

There is a far easier way to do it.

private var paused : boolean = false;

function OnGUI()
{
    if(Input.GetKeyDown(KeyCode.E))
		{
           // this line checks the state of the "paused" variable and then changes it to the other state
			paused = paused ? false:true; 
           // And here we're just changing the timecale
            if(paused)
			    Time.timeScale = 0;
		     else
			    Time.timeScale = 1; 

		}
		
}

Just a note to remember. when you type “paused = paused ? false:true”
the first state (in this case, false) has to be the state that you defined the paused variable as.

If you want this to be an onscreen button instead of a keyboard commannd, then change the if statement to

if(GUI.Button(Rect(10,10,100,100), "Pause"))

Oh! And if you plan on using the keycode instead of a pause button, remember to place the code in the Update function instead of OnGUI.