GetKeyDown escape for pausing (Toggle works once)

I have this script attached to my project. It's working but not fully. When i've paused the game once then unpaused it, It cannot be turned on and off again. Is there any way to make this script work permanently?

private var pauseToggle : boolean;

function Start () {
    pauseToggle = false;
}

function Update () {

        if (Input.GetKeyDown(KeyCode.Escape)) {
            if(pauseToggle) {
                Time.timeScale = 1;
            }
            else {
                pauseToggle = !pauseToggle;
                Time.timeScale = 0;
            }
        }
    }

Would be great with a quick answer, Up-Vote awaits! :)

1 Answer

1

Once pauseToggle is true, you're never unsetting it! You want to flip the pauseToggle boolean every time KeyCode.Escape is pressed, so move it out of the ifcheck for whether or not it's already paused.

Try this:

function Update ()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        if(pauseToggle)
            Time.timeScale = 1;
        else
            Time.timeScale = 0;

        pauseToggle = !pauseToggle;
    }
}

Omg, thats true -.-' Now i got annoyed by myself! How could i've missed that... However, the script is working now. Thank you very much Marowi! Up-Vote ;)