Pause Button Script

Hi, I am almost finished my game, but I still want a button to pause and unpause my game. I want it to be a .GUI button if you can. Please help! Thank you!

First search for Pause revealed this:

http://answers.unity3d.com/questions/15262/end-pause-game

Try click on the tag Pause or search as it helps keep the site cleaner :)

Here is a script that creates a pause/continue/quit GUI attach it to the Camera in your scene, or any GameObject with a GUILayer component attached.

var guiSkin: GUISkin;
var nativeVerticalResolution = 1200.0;
var isPaused : boolean = false;

function Update()
{

     if(Input.GetKeyDown("escape") && !isPaused)
   {
      print("Paused");
      Time.timeScale = 0.0;
      isPaused = true;
   }
   else if(Input.GetKeyDown("escape") && isPaused)
   {
      print("Unpaused");
      Time.timeScale = 1.0;
      isPaused = false;    
   } 
}

function OnGUI ()
{

    // Set up gui skin
    GUI.skin = guiSkin;

    // Our GUI is laid out for a 1920 x 1200 pixel display (16:10 aspect). The next line makes sure it rescales nicely to other resolutions.
    GUI.matrix = Matrix4x4.TRS (Vector3(0, 0, 0), Quaternion.identity, Vector3 (Screen.height / nativeVerticalResolution, Screen.height / nativeVerticalResolution, 1)); 

    if(isPaused)
    {
      // RenderSettings.fogDensity = 1;
      if(GUI.Button (Rect((Screen.width)/2,480,140,70), "Quit", "button2"))
      {
         print("Quit!");
         Application.Quit();
      }
      if(GUI.Button (Rect((Screen.width)/2,560,140,70), "Restart", "button2"))
      {
         print("Restart");
         Application.LoadLevel("SomeLevelHere");
         Time.timeScale = 1.0;
         isPaused = false;
      }
      if(GUI.Button (Rect((Screen.width)/2,640,140,70), "Continue", "button2"))
      {
         print("Continue");
         Time.timeScale = 1.0;
         isPaused = false;   
      }
   } 

}

@script AddComponentMenu ("GUI/Pause GUI")

This isn't an answer but you can change whether or not you want to exit the application with the quit button or if you want to make your player go to a main menu all you do is change this part Application.Quit(); and so change it to this Application.LoadLevel("name of main menu here");

what kinda script is it? Can we get the full code pls =)

I hope it helps you :slight_smile: js coce! Take it into the camera.

#pragma strict
var pause : boolean = false;
function OnGUI()

 {
 if (GUI.Button (Rect(Screen.width-55,60,50,50),"Pause"))


{


if (pause == false)
{ 
pause = true ;
Time.timeScale = 1 ;

return;
}

if (pause == true)
{
pause = false;
Time.timeScale = 0 ;
return;

}
}
}