- Home /
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.
Answer by karl_ · Sep 29, 2011 at 12:41 AM
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
Answer by Grady · Sep 29, 2011 at 12:21 AM
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
Lmao...What you're looking for:
public class P$$anonymous$$ : $$anonymous$$onoBehaviour
{
public GameObject Pause;
void Start()
{
Pause.IsActive(false);
}
void Update()
{
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
Pause.IsActive(true);
Time.timeScale = 0;
else
Pause.IsActive(false);
Time.timeScale = 1;
//$$anonymous$$ight 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. } }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pause : $$anonymous$$onoBehaviour {
public Transform canvas;
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Escape)) {
if (canvas.gameObject.activeInHierarchy == false) {
canvas.gameObject.SetActive (true);
Time.timeScale = 0;
} else
{
canvas.gameObject.SetActive (false);
Time.timeScale = 1;
}
}
}
}
Nvm, I fixed it, here's the code, all you need to do is drag the canvas containing the UI into this script and hit "ESC" and it'll pause.
hey there , i was training this code and unfortunately the if(canvas.gameobject........)
its not showing up with me i don't know why!!!!!
Answer by aurelienhamy · Apr 27, 2015 at 01:31 AM
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
}
Answer by Footknight · Nov 22, 2016 at 09:07 AM
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
Time scale Does not freeze mouse input
Answer by novaboxes · Apr 01, 2020 at 09:22 AM
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();
}
}
Your answer
Follow this Question
Related Questions
Unable to click UI Button (Unity 4.6) when TimeScale is 0 1 Answer
Bool wont change... #C 1 Answer
Pause Menu Text Not Rendering 0 Answers
Problem with Pause Menu because of WaitForSeconds 1 Answer
Zelda like pause menu 1 Answer