Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by PandawanFr · Mar 19, 2016 at 04:06 AM · menupausetimescale

Pause Menu Script TimeScale problems

Hey, so I am making a script for a pause menu, right now I am working on making the system to change between pause and unpause, and whenever I "click" on escape, it opens the pause menu, pauses the game, and in the second right afterwards, it unpauses.

Apparently, whenever I push the button, the delay goes to 1 and starts going down and once it reaches 0, it goes back immediately to 1 and disables the menu. I think it has to do with me keeping my finger on the key for more than a frame or a couple of frames because I don't get the problem if I push it quickly.

Here is my code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class PauseMenu : MonoBehaviour
 {
 
     bool opened = false;
     float oldTimeScale = 1.0f;
     float delay;
 
     void Update()
     {
         if (Time.timeScale != 1)
             delay -= Time.deltaTime * 1000;
         else
             delay -= Time.deltaTime;
 
         Debug.Log(oldTimeScale + " | " + delay + " | " + opened);
 
         if (delay < 0)
         {
             if (Input.GetAxis(GetPause()) > 0) //Get Some Input, different pause menu key based on player but doesn't really matter
             {
                 delay = 1;
                 opened = !opened;
             }
         }
 
         if (opened)
             DisplayMenu();
         else if (!opened)
             HideMenu();
 
     }
 
     void HideMenu()
     {
         GetComponent<Canvas>().enabled = false;
         Time.timeScale = oldTimeScale;
 
         Debug.Log("Hide");
 
     }
 
     void DisplayMenu()
     {
         GetComponent<Canvas>().enabled = true;
         if (Time.timeScale != 0.001f)
             oldTimeScale = Time.timeScale;
 
         Debug.Log("Display");
 
         Time.timeScale = 0.001f;
     }
 
 
     string GetPause()
     {
         return "PauseP" + PlayerPrefs.GetInt("mainPlayer"); ;
     }
 
 }
 

Thanks!

Comment
Add comment
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

1 Reply

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

Answer by simone9725 · Mar 19, 2016 at 02:23 PM

Well your script is a mess I'll do in this way

1)Create a panel with UI

2)Set the gameobject to false

3)Write something like this* In this way also the audio will be paused

4)Add a button for example an X and as function put OnClick pause the game so if you ve paused it automatically unpaused It's all :)


 using UnityEngine;
 using System.Collections;
 public class PauseMenuScript : MonoBehaviour 
 {
     public bool CanPause;
     public bool CanSound;
     public GameObject PausePanel;
     public string levelToLoad;
     void Start() {
         CanPause = true;
         CanSound = true;
         PausePanel.SetActive (false);
         AudioListener.pause = false;
     }
     
     public void pauseGame()
     {
         if (CanPause)
         {
             Time.timeScale = 0;
             AudioListener.pause = true;
             CanPause = false;
             PausePanel.SetActive (true);
         }
         else
         {
             Time.timeScale= 1;
             AudioListener.pause = false;
             CanPause = true;
             PausePanel.SetActive (false);
         }
     }
 }
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 PandawanFr · Mar 20, 2016 at 05:31 PM 0
Share

I edited your script a little bit, but it doesn't seem to work. CanPause stays at False.... I just added an Update so when you press Escape it switches CanPause and then calls pauseGame to update the menu. I added a Debug.Log but it only returns "Pausing? False" So I am guessing there's a problem somewhere, also, I did try to do CanPause = !CanPause; that didn't work either

 using UnityEngine;
 using System.Collections;
 public class Pause$$anonymous$$enu : $$anonymous$$onoBehaviour
 {
     public bool CanPause;
     public bool CanSound;
     public GameObject PausePanel;
 
     void Start()
     {
         CanPause = true;
         CanSound = true;
         PausePanel.SetActive(false);
         AudioListener.pause = false;
     }
 
     void Update ()
     {
         if(Input.GetAxis(GetPause()) > 0.75)
         {
             if (CanPause == true)
                 CanPause = false;
             else
                 CanPause = true;
 
             pauseGame();
         }
     }
 
     public void pauseGame()
     {
         Debug.Log("Pausing? " + CanPause);
         if (CanPause)
         {
             Time.timeScale = 0;
             AudioListener.pause = true;
             CanPause = false;
             PausePanel.SetActive(true);
         }
         else
         {
             Time.timeScale = 1;
             AudioListener.pause = false;
             CanPause = true;
             PausePanel.SetActive(false);
         }
     }
 
     string GetPause()
     {
         return "PauseP" + PlayerPrefs.GetInt("mainPlayer"); ;
     }
 }

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

52 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

Related Questions

Pause menu unpausing 1 Answer

How do i stop my mouse from being in first person when a pause menu appears? 0 Answers

Input System no button press when timescale = 0 1 Answer

Pause menu not called when ESC pressed 2 Answers

Problem setting Time.timeScale to 1 after being set to 0 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