How to make a pause menu in c#?

I’m trying to make a pause menu for my game but I don’t really understand how to make the game actually stop, I’ve looked into timescale but I don’t really understand how to make it work. I want it to have buttons that will allow the player to resume the game, look at options or credits, and to quit to the main menu. I’m using C#, and I haven’t found any tutorials that really explain how to create a pause menu. I already looked that the unity communities pause menu but that didn’t really help.

How would I be able to make it so it goes back to main menu as another button, my scene 0 is my main menu, scene 1 loading screen, and scene 2 is the actual game.

Go back to the main menu by calling Application.LoadLevel(0). Change 0 to the index of the level you want.

5 Answers

5

a very simple example:

using System;
using UnityEngine;

public class pause : MonoBehaviour
{
	bool paused = false;

	void Update()
	{
		if(Input.GetButtonDown("pauseButton"))
			paused = togglePause();
	}
	
	void OnGUI()
	{
		if(paused)
		{
			GUILayout.Label("Game is paused!");
			if(GUILayout.Button("Click me to unpause"))
				paused = togglePause();
		}
	}
	
	bool togglePause()
	{
		if(Time.timeScale == 0f)
		{
			Time.timeScale = 1f;
			return(false);
		}
		else
		{
			Time.timeScale = 0f;
			return(true);	
		}
	}
}

Works like a charm and if you add more buttons you can pause, restart by calling a scene, quit by calling Application.Quit call and its in C# finally.. Nice poster

I don't really know that much about C#, I'm more into JavaScript.

But, you could set it so that when your game is meant to be paused, just set the Time.timescale to 0 like this: `Time.timeScale == 0.0F`. (That is in C# by the way). Then just go `Time.timeScale == 1.0F` to set it back to normal.

Alternatively, if you're having a lot of trouble, I made a pause menu in JavaScript that you can download for free of the Asset Store.

It doesn't have a credits button, but it has a main menu button, but I'm sure that if you are OK with scripting, you could edit it!!!!!!!!!!!!!

Here is the link: http://u3d.as/content/grady-featherstone/pause-menu/1Zg

Also, you may like to check this out: http://unity3d.com/support/documentation/ScriptReference/Time-timeScale.html

It is the script reference for Time.timescale!

But here is a simple example in JavaScript on how you might stop the game when the escape button is pressed, and then unpause it:

private var pauseEnabled = false;
function Update(){

        //check if pause button (escape key) is pressed
        if(Input.GetKeyDown("escape")){

            //check if game is already paused       
            if(pauseEnabled == true){
                //unpause the game
                Time.timeScale = 1;         
            }

            //else if game isn't paused, then pause it
            else if(pauseEnabled == false){
                Time.timeScale = 0;
            }
        }
    }

Hope this helps you...

Comment back if you need more help...

-Grady

this is java script -_-

Lmao...What you're looking for: public class PM : MonoBehaviour { public GameObject Pause; void Start() { Pause.IsActive(false); } void Update() { if(Input.GetKeyDown(KeyCode.Escape)) Pause.IsActive(true); Time.timeScale = 0; else Pause.IsActive(false); Time.timeScale = 1; //Might wanna improvise as I'm in a rush, once you press ESC it'll pause //but won't stay paused unless you have your finger holding down ESC. } } }

That’s in French, but I’ve creat it alone !

using System.Linq;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class MenuPause : MonoBehaviour 
{

	#region Attributs

	private bool isPaused = false; // Permet de savoir si le jeu est en pause ou non.

	#endregion
	
	#region Proprietes
	#endregion
	
	#region Constructeur
	#endregion
	
	#region Methodes
	
	void Start () 
	{
	
	}
	
	
	void Update () 
	{
		// Si le joueur appuis sur Echap alors la valeur de isPaused devient le contraire.
		if(Input.GetKeyDown(KeyCode.Escape))
			isPaused = !isPaused;


		if(isPaused)
			Time.timeScale = 0f; // Le temps s'arrete
		
		else
			Time.timeScale = 1.0f; // Le temps reprend


	}

	void OnGUI ()
	{
		if(isPaused)
		{
		
			// Si le bouton est présser alors isPaused devient faux donc le jeu reprend.
			if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 - 60, 100, 40), "Continuer"))
			{
				isPaused = false;
			}

			// Si le bouton est présser alors on ferme completement le jeu ou charge la scene "Menu Principal
			// Dans le cas du bouton quitter il faut augmenter sa postion Y pour qu'il soit plus bas
			if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 + 00, 100, 40), "Recommencer"))
			{
				// Application.Quit(); 
				Application.LoadLevel("CarBigParcour");
			}

			if(GUI.Button(new Rect(Screen.width / 2 - 60, Screen.height / 2 + 60, 100, 40), "Quitter"))
			{
				Application.Quit(); 
				// Application.LoadLevel("CarBigParcour"); 

			

		}

		}
	}
	
	#endregion
}

Hello, I borrowed some of this code to create a toggle-able inventory for my game, my question is how do I make my cursor unlock and stay visible so I can interact with the inventory. My cursor is hidden by default but oddly enough I can’t seem to find any script responsible for hiding it, unless that’s just a Unity feature now… I tried using

CursorLockMode.None;
Cursor.visible = true;

after the keypress to open the menu, the cursor just flashes and remains locked and hidden…

well I believe time scale would freeze up mouse input as well, I'm not sure as to how you could go around this but maybe check if you can make the input immune to the scaling or perhaps a different method to pause you game, such asssss, I dunno, lol sorry I got nothin

is it What looking for

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class PauseMenu : MonoBehaviour
{

public static bool GameisPaused = false;
public GameObject pauseMenuUI;

void Start()
{
    pauseMenuUI.SetActive(false);
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Tab))
    {
        if (GameisPaused)
        {
            Resume();
        }
        else
        {
            Pause();
        }
    }
}
public void Resume()
{
    pauseMenuUI.SetActive(false);
    Time.timeScale = 1f;
    GameisPaused = false;
}
void Pause()
{
    pauseMenuUI.SetActive(true);
    Time.timeScale = 0f;
    GameisPaused = true;
}

public void loadmenu()
{        
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 0);
}

public void QuitGame()
{
    Debug.Log("QUIT!");
    Application.Quit();
}

}