3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ And what exactly is your problem with this code? It does not work - how? \$\endgroup\$
    – Nevermind
    Commented Mar 31, 2013 at 8:16
  • \$\begingroup\$ after pausing the game, it wont unpause \$\endgroup\$
    – SD1990
    Commented Apr 2, 2013 at 16:29

1 Answer 1

5
\$\begingroup\$

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;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ You can also use Input.GetKey() instead, which will return true only in one frame, until you release they key and press it again. \$\endgroup\$ Commented Apr 4, 2013 at 11:39
  • \$\begingroup\$ 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 \$\endgroup\$
    – Simurr
    Commented Apr 4, 2013 at 15:00
  • \$\begingroup\$ Oh yeah that's right \$\endgroup\$ Commented Apr 4, 2013 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .