Unpausing Game with GUI

I currently have this code in my OnGUI portion of a script. The game pauses just fine when I click the button the first time, but then won't unpause upon pressing the button again. What am I missing?

    if(GUI.Button(Rect(300,5,305,25),btnTexture) && !paused)
{
Debug.Log("HIT1");
    Time.timeScale=0;
    btnTexture=playTexture;
    paused=true;
}
else if(GUI.Button(Rect(300,5,305,25),btnTexture))
{
Debug.Log("HIT2");
    Time.timeScale=1;
    btnTexture=pauseTexture;
    paused=false;
}

I would rearrange it a bit:

private var paused = false;
....

    if(GUI.Button(Rect(300,5,305,25),btnTexture))
    {
      if (!paused)
      {
    Debug.Log("HIT1");
        Time.timeScale=0;
        btnTexture=playTexture;
        paused=true;
      }
      else
      {
    Debug.Log("HIT2");
        Time.timeScale=1;
        btnTexture=pauseTexture;
        paused=false;
      }
    }

The button is only registered as pressed once in OnGUI, so your else if can never fire. Instead, do this:

Edit: I was AFK and DaveA answered with the same code. This code looks formatted, though. ;-)

function OnGUI () {
    if (GUI.Button(Rect(300,5,305,25), btnTexture) {
        if (paused) {
            Debug.Log("Unpaused");
            btnTexture = pauseTexture;
            Time.timeScale = 1;
            paused = false;
        }
        else {
            Debug.Log("Paused");
            btnTexture = playTexture;
            Time.timeScale = 0;
            paused = true;
        }
    }
}

Thank you all of you this helped me too. :slight_smile:
I’m making a tower defense game and needed a pause for when the build menu is open and this did the trick.