Pausing/Unpausing the game, problems with time/boolean

So I’m trying to get the game to pause when the “Escape” key is pressed, but then unpause then the same “Escape” key is pressed. Here’s the code

using UnityEngine;
using System.Collections;

public class Menu : MonoBehaviour {
	public float menuDelay;
	public bool paused;

	// Use this for initialization
	void Start () {
		paused = false;
	
	}
	
	// Update is called once per frame
	void Update () {
		
		
		//Pause the Game
		if(Input.GetKeyUp(KeyCode.Escape)  paused == false){
			menuDelay = Time.realtimeSinceStartup +1;
			Time.timeScale = 0;
				if(menuDelay < Time.realtimeSinceStartup)
					paused = true;
			
		}
		
		//Unpause the Game
		//if(Input.GetKeyUp(KeyCode.Escape)  paused == true){
		//	Time.timeScale = 1;
		//	paused = false;
		//}		
	
	}
}

The problem I’m having though, is that after “menuDelay < Time.realtimeSinceStartup” the boolean “paused” is not being set to “true”.

Any idea’s on how to fix?

i think, that as your pause variable is false during the delay the delay constantly gets reset, that way it will never meet your goals, also you need to keep the escape button pressed for the timer to keep counting(or resetting right now), dont know if that is a meant behaviour.

What you might want to do is use a Coroutine:

using UnityEngine;
using System.Collections;

public class Menu : MonoBehaviour {
	public bool paused;

	// Use this for initialization
	void Start () {
		paused = false;
	
	}
	
	// Update is called once per frame
	void Update () {
		
		
		//Pause the Game
		if(Input.GetKeyUp(KeyCode.Escape)  paused == false){
			Time.timeScale = 0;
				if(menuDelay < Time.realtimeSinceStartup)
					StartCoroutine(showMenuDelayed(1));
			
		}
		
		//Unpause the Game
		//if(Input.GetKeyUp(KeyCode.Escape)  paused == true){
		//	Time.timeScale = 1;
		//	paused = false;
		//}		
	
	}
IEnumerator showMenuDelayed(float seconds){
yield return new WaitForSeconds(seconds);
paused=true;
}
}

If you dont know what a coroutine is, it basically creates a different update loop, as long as this is neccesary. This will loop untill it waited for, in your code, one second and then it sets paused to true.

Correct me if I’m wrong but won’t the WaitForSeconds command not work while I have timeScale set to 0?

Wont the menuDelay variable only be reset once escape is pressed again? I’m watching in my UNITY workspace and it only get’s reset once escape is pressed. But before the menuDelay gets reset the paused bool never changes to true, which is where I’m confused.

Thank you for the Coroutine information, I will definitely use it if I can’t find a work around without them.

EDIT: I fixed the problem, I just changed the conditions needed to unpause the game.

using UnityEngine;
using System.Collections;

public class Menu : MonoBehaviour {
	public float menuDelay;
	public bool paused;
	public float elapsedTime;
	public int minutes;
	public int seconds;
	public int fractions;


	// Use this for initialization
	void Start () {
		paused = true;
	
	}
	
	// Update is called once per frame
	void Update () {
		minutes = (int)elapsedTime / 60;
		seconds = (int)elapsedTime % 60;
		fractions = (int)(elapsedTime * 100) % 100;
		
		elapsedTime += Time.deltaTime;
		
		print(Time.realtimeSinceStartup);
		
		
		//Pause the Game
		if(Input.GetKeyUp(KeyCode.Escape)  paused == false){
			Time.timeScale = 0;
			menuDelay = Time.realtimeSinceStartup +1;
			paused = true;
			
			
		}
		
		//Unpause the Game
		if(Input.GetKeyUp(KeyCode.Escape)  menuDelay < Time.realtimeSinceStartup){
			Time.timeScale = 1;		
			paused = false;
		}		
	
	}
}