- Home /
Pausing and unpausing the game with one button
I have a script that I want if a button is clicked if the game is paused to unpause it and if it is not paused to pause the game. Here is my script so far. Right now it pauses and doesn't unpause once I press the same button that I paused it with.
using System.Collections;
using UnityEngine.UI;
public class PauseGame : MonoBehaviour
{
public KeyCode pauseKeyCode;
[SerializeField] public Canvas pauseMenuGroup;
[SerializeField] private int i;
[SerializeField] private bool isPaused;
void Start ()
{
pauseMenuGroup.enabled = false;
}
void Update ()
{
if(Input.GetKeyDown(pauseKeyCode) && !isPaused)
{
Pause();
i++;
}
if(i > 1 && Input.GetKeyDown(pauseKeyCode) && isPaused)
{
Unpause();
i = 0;
}
}
void Pause ()
{
Time.timeScale = 0f;
pauseMenuGroup.enabled = true;
isPaused = true;
}
void Unpause ()
{
Time.timeScale = 1f;
pauseMenuGroup.enabled = false;
isPaused = false;
}
}
Answer by Pindwin · Jan 03, 2015 at 11:24 AM
Thats because you are checking
i > 1
to unpause. i is equal one, so the condition equals false. You need to check if it's > 0.
You could also go like:
if(Input.GetKeyDown(pauseKeyCode))
{
if(!isPaused) Pause();
else Unpause();
}
Answer by Ekta-Mehta-D · Jan 03, 2015 at 11:28 AM
Hii..
There is no need to use "i" variable.. Remove i > 1 comparison from if condition.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do i have a pause after i resume game C# 6.4 0 Answers
Zelda like pause menu 1 Answer
Creating Pause Game Menu 1 Answer
Sound on pause it doesn't works. 2 Answers