Pause Script

A while ago i made this pause script:

private var gamePaused = false;
Screen.showCursor = false;
function Update(){
	if (gamePaused == false){
		if(Input.GetKey(KeyCode.P)){
			gamePaused = true;
			Screen.showCursor = true;
			Time.timeScale = 0;
		}
	}
	if (gamePaused == true){
		if(Input.GetKey(KeyCode.O)){
			gamePaused = false;
			Screen.showCursor = false;
			Time.timeScale = 1;
		}
	}
}

well. It works fine, but i really want it to be the same key to pause and unpause. When i change the KeyCode.O to KeyCode.P, it doesn’t work. I think this is because when they are the same key, it pauses and then unpauses instantly, so it pretty much has no effect. I have put a debug log in both of the input if statements and they both work.

use else if in the second statement instead of just if. That way, it can only ever test one if per update and if that still isn’t working, keep the else if idea and turn GetKey to GetKeyUp because I think GetKey just keeps firing while it looks like GetKeyUp just returns true for the frame the key was released.

Hope that helps.