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.

Try this:

public class Pauser : MonoBehaviour {
	public bool gamePuased = false;
	
	//Just to make sure a previous scene didn't mess something up....
	void Awake(){
		Time.timeScale = 1;
		Screen.showCursor = true;
	}
	
	void Update(){
		if(Input.GetKeyUp(KeyCode.P)){
				gamePuased = !gamePuased;
				Time.timeScale = Mathf.Abs(Time.timeScale - 1);
				Screen.showCursor = !Screen.showCursor;		
		}		
	}	
}