I am adding a pause menu in my game and its going pretty well. But I just can’t get my head around this problem right now.
Im using a canvas that is deactivated at the start and when you press esc it should pop up. That worked, but when I wanted it to go away when I press esc again it started to flicker if I held the esc key.
Here’s my code:
using UnityEngine;
using System.Collections;
public class Pause : MonoBehaviour {
public GameObject Canvas;
public GameObject Camera;
bool Paused = false;
void Start(){
Canvas.gameObject.SetActive (false);
}
void Update () {
if (Input.GetKey ("escape")) {
if(Paused == true){
Time.timeScale = 1.0f;
Canvas.gameObject.SetActive (false);
Screen.showCursor = false;
Screen.lockCursor = true;
Camera.audio.Play ();
Paused = false;
} else {
Time.timeScale = 0.0f;
Canvas.gameObject.SetActive (true);
Screen.showCursor = true;
Screen.lockCursor = false;
Camera.audio.Pause ();
Paused = true;
}
}
}
public void Resume(){
Time.timeScale = 1.0f;
Canvas.gameObject.SetActive (false);
Screen.showCursor = false;
Screen.lockCursor = true;
Camera.audio.Play ();
}
}
The resume function at the bottom is for the resume button. So you could resume with the button or with esc.
Help would be appreciated.