- Home /
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;
}
}
}
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!
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
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.
works perfectly now thank you for all your help:)
If everything is good please mark this as the answer and have a nice day.
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.
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();
}