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;
}
}