Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
And what exactly is your problem with this code? It does not work - how? –  Nevermind Mar 31 '13 at 8:16
    
after pausing the game, it wont unpause –  SD1990 Apr 2 '13 at 16:29
add comment

1 Answer 1

up vote 4 down vote accepted

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;
    }
}
share|improve this answer
    
You can also use Input.GetKey() instead, which will return true only in one frame, until you release they key and press it again. –  Anomalous Underdog Apr 4 '13 at 11:39
    
Input.GetKey() returns true every frame the key is held down, not once. It would make the problem much worse. Your thinking of GetKeyDown or GetKeyUp –  Simurr Apr 4 '13 at 15:00
    
Oh yeah that's right –  Anomalous Underdog Apr 4 '13 at 15:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.