PauseButton

When the player pauses the game there is an option to quit. But if the game is restarted it stays paused and you have to press p before being able to play. here’s my script:

static var timeScale : float =1.0;
// Toggles the time scale between 1 and 0.7
// whenever the user hits the Fire1 button.
var Ispaused : boolean =false;

function Update () {
    if (Input.GetKeyDown ("p")) {
        if (Time.timeScale == 1.0) {
            Time.timeScale = 0;
            Ispaused=true;
        }
        else {
            Time.timeScale = 1.0;
            Ispaused=false;
        }
        Time.fixedDeltaTime = 0.02 * Time.timeScale;
    }


}

function OnGUI () {
    if(Ispaused){
    // Make a background box
    GUI.Box (Rect (200,200,100,90), "Pause Menu");

    if (GUI.Button (Rect (200,220,80,20), "Quit")) {
        Application.LoadLevel (0);
        Ispaused=false;
    }
    }

}

How do i fix this?
thanks! :smile:

Questions :

  • You never use your static variable timeScale. Is it normal ?
  • When the game is restarted, what does it means ? You press p to enter in pause mode, and p again to exit. Does it works ?

Sorry if you already solved this, but I found your thread while searching for something else.

I think you need to change the button code from this:

    if (GUI.Button (Rect (200,220,80,20), "Quit")) {
        Application.LoadLevel (0);
        Ispaused=false;
    }

to this:

    if (GUI.Button (Rect (200,220,80,20), "Quit")) {
        Ispaused=false;
        Time.timeScale = 1.0;
        Application.LoadLevel (0);
    }

I think this is because Time.timeScale is held in the engine and when you “Quit” and load up level(0), it is still holding the zero value you left in it, so you then need to hit the “p” button to reset it to the 1.0 value.

I don’t know if its necessary, but I picked up somewhere that its a good idea to manipulate Time.timeScale and Time.fixedDeltaTime together.

ex:

function Freeze ()
{
    Time.timeScale = 0.0;
    Time.fixedDeltaTime = 0.02 * Time.timeScale;
}