• 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
Question by Mindrobot · Sep 07, 2018 at 01:59 AM · scripting problempause menu

Pause menu Script not working

Hey guys i was writing a script to pause the game and open the pause menu but my script doesn't seem to work properly, i added a debug to print the timescale and it prints 0 but doesn't pause the game. I haven't been able to figure out why. can someone please help me?

 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class PauseMenu : MonoBehaviour
 {
     public GameObject PauseUI;
 
     public static bool Paused = false;
 
     void Resume()
     {
         PauseUI.SetActive(false);
         Time.timeScale = 1f;
         Paused = false;
     }
 
     void Pause()
     {
         PauseUI.SetActive(true);
         Time.timeScale = 0f;
         Paused = true;
         Debug.Log(Time.timeScale);
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             if (Paused == false)
             {
                 Pause();
 
             }
             if (Paused == true)
             {
                 Resume();
 
             }
         }
     }
 
     public void BackToMenu()
     {
         SceneManager.LoadScene("GameMenu");
     }
 
 
 
 }
Comment

People who like this

0 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by yummy81 · Sep 07, 2018 at 10:58 AM

The problem with your code lies here, in the Update method:

 void Update()
      {
          if (Input.GetKeyDown(KeyCode.Escape))
          {
              if (Paused == false)
              {
                  Pause();
  
              }
              if (Paused == true)
              {
                  Resume();
  
              }
          }
      }

So, when you press the Escape button, your code checks whether the Paused boolean is false. If it is, then it invokes the Pause method. Inside Pause method, the Paused boolean is assigned true and timeScale is assigned 0f. Subsequently, your code exits the Pause method and comes back to the Update method where - and this is where the problem lies - it checks once again if the Paused boolean is true. Because it is true, then the Resume method is invoked, where the Paused boolean is assigned false, and timeScale is assigned 1f. And that's why it do not work. The reason why you see only zero in the console is that, you added Debug.Log only in the Pause method. Try to add it in the Resume method, and you will see that those two methods are invoked simultaneously. It's very easy to make it work. Simply add "else" before "if (Paused == true)":

 void Update()
       {
           if (Input.GetKeyDown(KeyCode.Escape))
           {
               if (Paused == false)
               {
                   Pause();
   
               } else if (Paused == true)
               {
                   Resume();
   
               }
           }
       }
 
Comment
Mindrobot

People who like this

1 Show 0 · 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

Answer by KittenSnipes · Sep 07, 2018 at 02:30 AM

@Mindrobot It is because you never actually use the Boolean to pause or resume

  using UnityEngine;
  using UnityEngine.SceneManagement;
  
  public class PauseMenu : MonoBehaviour
  {
      public GameObject PauseUI;
  
      public static bool Paused = false;
  
      void Resume()
      {
          PauseUI.SetActive(false);
          Time.timeScale = 1f;
      }
  
      void Pause()
      {
          PauseUI.SetActive(true);
          Time.timeScale = 0f;
          Debug.Log(Time.timeScale);
      }
  
      void Update()
      {
          if (Input.GetKeyDown(KeyCode.Escape))
          {
               Paused = !Paused;
          }
            if (Paused == false)
              {
                  Pause();
  
              }
              if (Paused == true)
              {
                  Resume();
  
              }
      }
  
      public void BackToMenu()
      {
          SceneManager.LoadScene("GameMenu");
      }
  
  
  
  }
Comment
Mindrobot

People who like this

1 Show 0 · 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

Answer by Sonicbulletwolf · Sep 07, 2018 at 02:36 AM

Hope this helps!

     [SerializeField] // See in Inspector
     private GameObject PauseUI; // Game Object
 
     [SerializeField]
     private bool gamePaused; // Enable / Disable
 
 
     void EnablePauseMenu()
     {
         Time.timeScale = 0f;
         PauseUI.SetActive(true);
     }
 
     // We don't need to set the variable up
     // Since we already switch it every
     // Time we Hit 'Esc' it switches
     // for us!
 
     void DisablePauseMenu()
     {
         Time.timeScale = 1f;
         PauseUI.SetActive(false);
         Debug.Log(Time.timeScale);
     }
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             /// This snipit of codes changes the variable
             /// to be the opposite of what is was
             /// such as if I hit 'Esc' if its false
             /// it turns to true
             gamePaused = !gamePaused;
         }
 
         // Same as gamePause == true
         // (!gamePaused) is same as gamePause == false
         // Checking if its true and paused already switch
         // it to false, else if its not true then enable
         // the UI.
         if (gamePaused)
         {
             // Checking 
             DisablePauseMenu();
         }
         else
         {
 
             EnablePauseMenu();
         }
     }
 
     public void BackToMenu()
     {
         SceneManager.LoadScene("GameMenu");
     }
Comment
Mindrobot

People who like this

1 Show 0 · 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

Answer by Mindrobot · Sep 07, 2018 at 12:04 PM

Thank you @yummy81 , @Sonicbulletwolf and @KittenSnipes a lot for helping me understanding the many problems in my code. :)

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

C# problem with pause menu 2 Answers

Creating a Pause Menu with DOTween 1 Answer

Audio plays only once and not playing on click of button again(using audio manager script) 0 Answers

I need to freeze Camera movement from FirstPersonController 1 Answer

Error with pause menu script. 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