Greetings,
I’m trying to add a pause screen to my 3D space shooter. I followed a tutorial which said to use Time.timeScale = 0 to accomplish this.
I created a PauseController script and put it on an object in my game. It correctly pauses the game and the music but I am unable to get keyboard input to unpause the game in that state. I even added a coroutine to check for the keyboard input but my ResumeGame() function is never getting hit.
Here’s the code for the script. How can I fix this?
using System.Collections;
using UnityEngine;
public class PauseManager : MonoBehaviour
{
public static bool gamePaused = false;
void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.F9))
{
TogglePauseState();
}
}
public void PauseGame()
{
Debug.Log("Pausing game");
AudioListener.pause = true;
Time.timeScale = 0;
gamePaused = true;
StartCoroutine(CheckForUnpause());
}
public void ResumeGame()
{
Debug.Log("Resuming game");
AudioListener.pause = false;
Time.timeScale = 1;
gamePaused = false;
StopCoroutine(CheckForUnpause());
}
public void TogglePauseState()
{
if (gamePaused)
{
ResumeGame();
}
else
{
PauseGame();
}
}
IEnumerator CheckForUnpause()
{
while (gamePaused)
{
yield return new WaitForSeconds(0.1f);
if (Input.GetKeyDown(KeyCode.F9))
{
ResumeGame();
}
}
}
}
Thanks,
Greg.