Pausing and resuming in Unity

Pausing and resuming in Unity

I've been writing this code quite late so please bear with me.
I'm trying to pause the game and show a menu when I'm inside a collider and then when i press a key it should unpause the game and continue but it wont and i cant figure out why.
Here's the very simple (in my eyes) code: 
var savedTimeScale : float;
var playerStatus : Player_Status;
var inTheZone : boolean = false;
private var startTime: float = 0.1; 
var start:GameObject;

enum Upgrade { None,Main }
private var currentPage:Upgrade;

function Start()
{
    currentPage = Upgrade.None;
}

//function for ontriggerenter and pressing button to show menu
function OnTriggerEnter(collider:Collider)
{ 
    playerStatus = collider.GetComponent(Player_Status);
    if(playerStatus == null) return;

    inTheZone = true;
}

function OnTriggerExit(collider: Collider)
{
    playerStatus = collider.GetComponent(Player_Status);
    if(playerStatus == null) return;

    inTheZone = false;
}

function Pause()
{
    savedTimeScale = Time.timeScale;
    Time.timeScale = 0;
    AudioListener.pause = true;
    currentPage = Upgrade.Main;
}

function UnPause()
{
    Time.timeScale = savedTimeScale;
    AudioListener.pause = false;
    currentPage = Upgrade.None;
    if(IsBeginning() && start != null)
    {
        start.SetActive(true);
    }
}

function IsGamePaused()
{
    return Time.timeScale == 0;
}

function OnGUI()
{
    if(inTheZone == true && Input.GetKeyDown(KeyCode.E))
    {
        Pause();
    }

    if(Input.GetKeyDown(KeyCode.N))
    {
        UnPause();
    }

    if(IsGamePaused() && !IsBeginning())
    {
        UpgradeMenu();
    }
}

function UpgradeMenu()
{
    BeginPage(200,200);
    GUILayout.Button("Hello");
    EndPage();
}

function BeginPage(width, height)
{
    //GUI.DrawTexture
    GUILayout.BeginArea(Rect((Screen.width-width)/2,(Screen.height-height)/2,width,height));
}
function EndPage()
{
    GUILayout.EndArea();
    ShowBackButton();
}
function ShowBackButton()
{
    if(GUI.Button(Rect(20, Screen.height-50,50,20), "Resume"))
    {
        UnPause();
    }
}

function IsBeginning()
{
    return Time.time < startTime;
}

I think it's just a case of a new set of eyes. But by my logic this should work.

Solutions/Answers:

Answer 1:

The problem is with your Input detection in OnGUI. Using OnGUI to trigger the Pause function can cause it to be called several times in one frame, which sets your savedTimescale to 0, thus UnPause just resets the Timescale to 0.

On my machine Pause (GetKeyDown(E)) was being called 4 times when the key was pressed.

Use Update() for Input checking, add some boolean checks for whether you are paused rather than just the Timescale value or use GUI Buttons for pause/unpause rather than Input key detection.

function Update()
{
  if(inTheZone == true && Input.GetKeyDown(KeyCode.E))
  {
      Pause();
  }

  if(Input.GetKeyDown(KeyCode.N))
  {
      UnPause();
  }
}

Addendum ::

Thinking about it more, you should probably (always?) use a boolean to check if you are paused as well. Hitting 'E' twice would cause the same issue.

var paused : bool = false;
function Pause()
{
    if ( !paused )
    {
        savedTimeScale = Time.timeScale;
        Time.timeScale = 0;
        AudioListener.pause = true;
        currentPage = Upgrade.Main;
        paused = true;
    }
}

function UnPause()
{
    if ( paused )
    {
        Time.timeScale = savedTimeScale;
        AudioListener.pause = false;
        currentPage = Upgrade.None;
        if(IsBeginning() && start != null)
        {
            start.SetActive(true);
        }
        paused = false;
    }
}

References