Hey Guys!
I have recently switched from JS to C# which I just found out required me to go from Update() to IEnumerator when making my Pause Menu script…
So, can someone help me correct my current broken script, so that the game sets Menu active, while freezing the game on first press of the escape key, while doing the opposite on the second push?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Pause : MonoBehaviour
{
public GameObject Menu;
bool Freeze = false;
void Start ()
{
}
private IEnumerator Pausemenu()
{
while (Input.GetKey(KeyCode.Escape) && Freeze == false)
{
Menu.SetActive(true);
Time.timeScale = 0.0f;
yield return new WaitForSeconds(1);
Freeze = true;
}
while (Input.GetKey(KeyCode.Escape) && Freeze == true)
{
Menu.SetActive(false);
Time.timeScale = 1;
yield return new WaitForSeconds(1);
Freeze = false;
}
}
}
Sorry for late reply! Try using Input.GeyKeyDown() instead of Input.GetKey() because Input.GetKey() returns true when you hold it. Input.GetKeyDown() returns true once. Try doing that without using IEnumerator.