- Home /
Game won't unpause after pausing
Pressing 'escape' key pauses the game without no issue. But it can't be unpaused again. Neither the button in the panel nor pressing the 'escape' key again work.. Game simply can't be unpaused once it is paused. Following is the code.. Any help is much appreciated. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
public class PauseGame : MonoBehaviour {
public bool paused = false;
public GameObject thePlayer;
public GameObject pausedMenu;
void FixedUpdate () {
if (Input.GetButtonDown("Cancel")) {
if (paused == false) {
Time.timeScale = 0;
paused = true;
pausedMenu.SetActive(true);
thePlayer.GetComponent<FirstPersonController>().enabled = false;
Cursor.visible = true;
}
else {
thePlayer.GetComponent<FirstPersonController>().enabled = true;
paused = false;
pausedMenu.SetActive(false);
Time.timeScale = 1;
}
}
}
public void UnPauseGame() {
thePlayer.GetComponent<FirstPersonController>().enabled = true;
paused = false;
pausedMenu.SetActive(false);
Time.timeScale = 1;
}
}
Well the first thing I would suggest you to remove the code from the FixedUpdate() and rather use Update() as "FixedUpdate" is mostly used for the physics purpose. You are assigning Timescale = 0 when "paused == false" while in UnPauseGame() you are assigning "pause = false".
Here's a little code that I would suggest you to replace.
bool paused = false;
void Update()
{
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Escape))
{
if(paused)
UnPause();
else
PauseGame();
}
}
public void PauseGame()
{
paused = true;
Time.timeScale = 0;
paused$$anonymous$$enu.SetActive(true);
thePlayer.GetComponent<FirstPersonController>().enabled = false;
}
public void UnPause()
{
paused = false;
Time.timeScale = 1;
thePlayer.GetComponent<FirstPersonController>().enabled = true;
paused$$anonymous$$enu.SetActive(false);
}
Answer by Arcane94 · Oct 08, 2018 at 03:11 AM
@imran-arif011 Thanks.. Just using 'Update" function instead of "FixedUpdate" fixed the issue. The code I posted in the question works perfectly fine. I tried your code too. It doesn't work properly. Kinda gives unpredictable pauses when 'escape' pressed. Anyway, thanks for the answer. Appreciate it.
Your answer
Follow this Question
Related Questions
Time.timeScale seems doesn't work with Update 1 Answer
need help with locking the camera in Pause Menu 1 Answer
Pausing the game. Problem 1 Answer
Escape key not working 1 Answer
Pause Menu open/close on key 1 Answer