-1
\$\begingroup\$

I got my pause menu working by using this code:

using UnityEngine;
using System.Collections;

public class PauseMenu : MonoBehaviour {

public GameObject pauseUI;

private bool paused = false;

void Start()
{
    pauseUI.SetActive(false);
}

void Update()
{
    if(Input.GetButtonDown("Pause"))
    {
        paused = !paused;
    }

    if(paused)
    {
        pauseUI.SetActive(true);
        Time.timeScale = 0;
    }

    if(!paused)
    {
        pauseUI.SetActive(false);
        Time.timeScale = 1;

    }
}

public void Resume()
    {
    paused = false;
    }

public void Restart()
{
    Application.LoadLevel(Application.loadedLevel);
}


public void MainMenu()
{
    // please write a main menu Luke! (using Loadlevel) Then the Main Menu button will work
    Application.LoadLevel(0);
}


public void Quit()
{
    Application.Quit();
}
}

sorry for the appearance of the code.

And I wrote a start Menu using this, which includes a background image and 2 buttons.

    using UnityEngine;
using System.Collections;

public class StartMenu : MonoBehaviour {

public GameObject StartUI;
public GameObject startScreenUI;

private bool startit = true;
private bool background = true;


private void Start()
{
    StartUI.SetActive(true);
    startScreenUI.SetActive(true);
    Time.timeScale = 0;

}
void Update()
{

    if (startit)
    {
        Time.timeScale = 0;
    }

    if (!startit)
    {
        Time.timeScale = 1;
        StartUI.SetActive(false);
        startScreenUI.SetActive(false);
    }

}

public void Play()
{
    startit = false;
    background = false;
}

public void Quit()
{
    Application.Quit();
}

}

The problem is, once I have included the Start screen and the Pause screen together, the pause screen, although showing up and the buttons working correctly does not pause the game.

I am thinking perhaps a public function, that would be shared between the PauseMenu and the StartMenu script should do the trick?

or should I combine the two scripts into one and manage them together?

I think the problem is related to the StartMenu script automatically updating and setting the Time.timescale to 1 when the start screen is not activated when Update() is called.

I am not quite sure how to go about solving this issue. It already was a real pain to get the start screen image to disappear when the "play" button was pressed and the Play() function called because of the way the UI system works. If there is a work-around that would not involve me having to move the UI around again that would be great!

\$\endgroup\$
  • \$\begingroup\$ I am sorry to say, but honestly I could not understand your code, could you please explain the problem so we can get it together? \$\endgroup\$ – Hamza Hasan Dec 14 '15 at 21:04
  • \$\begingroup\$ One more thing, NEVER NEVER do anything in Update() which should call once or calling them once is enough. NEVER! \$\endgroup\$ – Hamza Hasan Dec 14 '15 at 21:05
  • \$\begingroup\$ I'm sorry I really should add more comments and follow conventions better. Ill certainly take your advice in regards to Update(). \$\endgroup\$ – Luke Taylor Dec 15 '15 at 0:42
0
\$\begingroup\$

The startmenu seems to be the source of this bug indeed. Apparently, update on it is called after the update on the pausemenu. The pausemenu will set the timescale to 0 because paused is true, but then the startmenu is changing this back to 1 because startit is false.

I don't see a need for updating the timescale every update. If you were to change it at the moment the start-or pausescreen (dis)appears instead, it should solve your issue. E.g.:

public void Play()
{
    startit = false;
    background = false;
    Time.timeScale = 1;
}

The same goes for any related actions you are performing in the update calls.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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