Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by matthew_tavares1 · Sep 29, 2011 at 12:08 AM · c#menupause

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kc01 · Aug 13, 2014 at 07:03 AM 0
Share

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.

avatar image TheDDestroyer12 · Jan 06, 2015 at 02:37 PM 0
Share

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

5 Replies

· Add your reply
  • Sort: 
avatar image
9
Best Answer

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);    
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image supamigit · Jun 06, 2014 at 10:14 AM 0
Share

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

avatar image
3

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image omegacraft · Oct 11, 2014 at 12:55 PM 0
Share

this is java script -_-

avatar image IMemeManI · Jun 06, 2017 at 12:21 PM 0
Share

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

avatar image IMemeManI IMemeManI · Jun 06, 2017 at 01:02 PM 0
Share
 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.

avatar image manal1122 IMemeManI · Mar 16, 2019 at 09:26 AM 0
Share

hey there , i was training this code and unfortunately the if(canvas.gameobject........)

its not showing up with me i don't know why!!!!!

avatar image
0

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
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bazoo_Studios · Jun 18, 2017 at 02:17 AM 0
Share

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

avatar image thekinghamza Bazoo_Studios · Jul 24, 2018 at 01:29 PM 0
Share

Time scale Does not freeze mouse input

avatar image
0

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

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Pause Menu Not Pausing 3 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges