x


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! :)

more ▼

asked Apr 02 '11 at 01:56 AM

N0tiC gravatar image

N0tiC
5 2 2 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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;
    }
}
more ▼

answered Apr 02 '11 at 02:04 AM

Marowi gravatar image

Marowi
4.9k 4 14 53

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 ;)

Apr 02 '11 at 02:08 AM N0tiC

No troubles - sometimes you need fresh eyes to look at a problem!

Apr 02 '11 at 02:13 AM Marowi
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x268
x169

asked: Apr 02 '11 at 01:56 AM

Seen: 1320 times

Last Updated: Apr 02 '11 at 01:56 AM