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 /
avatar image
0
Question by ahamelin9 · Mar 05, 2019 at 02:59 PM · c#unityeditorprogrammingplatformerpause menu

Pause System not working :/

  I have tried many ways for my code to work and it is honestly a headache at this point any new codes or issues to fix my code would be extremely helpful. I have no idea what is wrong with my code but it is not working almost at all and yes I have everything set up in unity for it to work I believe it is with this code or the sample code Unity uses for my player controller. Here is the code thanks in advance for the help if you do try to help <3



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

public class Pause : MonoBehaviour {

 public static bool gameIsPaused = true;
 public KeyCode pause;
 public m_MoveDir PlayerMove;
 public GameObject pauseMenuUI;

 public class m_MoveDir
 {
     internal bool enabled;
 }

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

     if(SceneManager.GetActiveScene() != SceneManager.GetSceneByName("menu"))
     {
         PlayerMove = GameObject.FindGameObjectWithTag("Player 1").GetComponent<m_MoveDir>
         ();
     }
 }

 private void Update()
 {
     StartCoroutine(Wait());
 }

 IEnumerator Wait()
 {
     if(Input.GetKey(pause))
     {
         if (gameIsPaused)
         {
             yield return new WaitForSeconds(1 / 4);
             Resume();
         }
         else if(!gameIsPaused)
         {
             yield return new WaitForSeconds(1 / 4);
             Resume();
         }
     }
 }

 public void Resume ()
 {
     pauseMenuUI.SetActive(false);
     Time.timeScale = 1f;
     gameIsPaused = false;
     PlayerMove.enabled = true;
 }

 void DoPause ()
 {
     pauseMenuUI.SetActive(true);
     Time.timeScale = 0f;
     gameIsPaused = true;
     PlayerMove.enabled = true;

 }

 public void LoadMenu()
 {
     SceneManager.LoadScene("menu", LoadSceneMode.Single);
     if (gameIsPaused)
         if(gameIsPaused)
         {
             pauseMenuUI.SetActive(false);
             gameIsPaused = false;
             Application.Quit();
             SceneManager.LoadScene("menu");
             Time.timeScale = 1f;
             PlayerMove.enabled = true;
         }
 }

}

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

2 Replies

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

Answer by Sazails · Mar 05, 2019 at 03:57 PM

@xxmariofer You pause then it unpauses after 2 seconds...

ANYWAYS

Look if you want to create a pause menu it is simple!

 using UnityEngine;
 
 public class PauseMenu : MonoBehaviour
 {
     public GameObject pauseContents;
     private bool isPaused = false;
 
     private void Start()
     {
         pauseContents.SetActive(false);
     }
 
     private void Update()
     {
         if(!isPaused)
         {
             Cursor.lockState = CursorLockMode.Locked;
             Cursor.visible = false;
         }
 
         if(Input.GetKeyDown(KeyCode.Escape))
         {
             Toggle();
         }
     }
 
     void Toggle()
     {
         isPaused = !isPaused;
         if (isPaused)
         {
             Time.timeScale = 0;
             pauseContents.SetActive(true);
             Cursor.lockState = CursorLockMode.None;
             Cursor.visible = true;
         }
         else
         {
             pauseContents.SetActive(false);
             Cursor.lockState = CursorLockMode.Locked;
             Time.timeScale = 1;
         }
     }
 }

Now in every script that needs to be paused, just put this at the start of any Update() function

 if (Time.timeScale < 0.2f)
             return;

Your welcome!

Comment
Add comment · Show 5 · 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 ahamelin9 · Mar 05, 2019 at 04:04 PM 0
Share

thank you so much!! it almost works perfectly like everything is good up until i need to go back into the game it think it is still paused any idea? (this is an edit) it thinks it is still paused but when i hit esc again then its cool but like obviously thats a small issue and im a lil OCD lol

avatar image Sazails ahamelin9 · Mar 05, 2019 at 04:10 PM 1
Share

It shouldn't be paused if done correctly. 1) $$anonymous$$ake sure you assigned the parent gameobject of the pause menu UI into the pauseContents gameobject in the pause$$anonymous$$enu script. 2) $$anonymous$$ake sure you have the following

 if (Time.timeScale < 0.2f)
              return;

At the start of each update function that you would want to be paused, so character movement, but maybe don't put it in stuff such as real-time clock.

3) If you experience pause, it might be an issue with your game time.timeScale. So change pause$$anonymous$$enu script Start() function to this:

 private void Start()
      {
          pauseContents.SetActive(false);
          Time.timeScale = 1;
      }

This should work. If you want more help, send me your project to zaidimaifl@gmail.com via google drive and I will see if I can help.

avatar image ahamelin9 Sazails · Mar 06, 2019 at 02:37 PM 0
Share

works perfectly now thank you for all your help:)

avatar image Sazails ahamelin9 · Mar 05, 2019 at 04:21 PM 0
Share

If everything is good please mark this as the answer and have a nice day.

avatar image xxmariofer · Mar 05, 2019 at 04:15 PM 0
Share

hello, i was unpausing after 2 secs because in his code he was waiting 1/4 secs before unpausing i know is a weird behaviour i just tried to set up an example without having to touch much of his code, but your set up is better than his tried.

avatar image
0

Answer by xxmariofer · Mar 05, 2019 at 03:12 PM

you are doing it in a really extrange way, test this code just overriding your update and corroutine

 private void Update()
 {
     if (Input.GetKey(pause))
     {
         StartCoroutine(Wait());
     }
 }

 IEnumerator Wait()
 {
     DoPause();
     yield return new WaitForSeconds(2);
     Resume();
 }
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

106 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

Related Questions

Multiple Cars not working 1 Answer

How do I get Unity to report "You have 1 bullet remaining" instead of "You have 1 bullets remaining"? 2 Answers

I need help with registering blocks in my 3d tetris game.,trying to make a 3d tetris and need help registering subBlocks? 0 Answers

Pause menu not working??? 1 Answer

How would I go about create multiple cursors controlled by multiple players 1 Answer


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