0

I'd like to know how I can pause and unpause menu from the same button using the mouse pointer when I click on it.

Lets say I have this. C#

void Update () {
    if (Button_Pause.OnPointerClick()) {
        if(!active){
            PauseGame();
        }
        else{
            ResumeGame();
        }
        active = !active;
    }
}

public void PauseGame()
{
    Button_Pause = Button_Pause.GetComponent<Button> ();
    Canvas_PauseMenu.enabled = true;
    Button_Exit.enabled = true;
    Button_Pause.enabled = true;
}

public void ResumeGame()
{
    Canvas_PauseMenu.enabled = false;
    Button_Exit.enabled = false;
    Button_Pause.enabled = false;
}

In the first line, where I call the OnPointerClick I'm just guessing because I don't know what to do. What I've searched around, using click to show something it's having a TimeScale or something like that.

¿Can anyone help moi? Please.

d4Rk
  • 6,622
  • 5
  • 46
  • 60
Sean Sabe
  • 79
  • 9
  • It is pretty simple. Check if the button is clicked then inside the check check to resume or pause with the ennabled bool you already have. If (Canvas_PauseMenu.enabled) { ResumeGame(); } else { PauseGame(); } – deathismyfriend Jun 06 '15 at 04:10

2 Answers2

0

Add a listener for your button and in your pause script set timescale to zero to pause the game

[SerializeField] private Button MyButton = null; // assign in the editor

void Start() { MyButton.onClick.AddListener(() => { pause();});
}

void pause(){
        if (Time.timeScale == 1)
         {
             Time.timeScale = 0;
         }
         else
         {
             Time.timeScale = 1;
         }

 }
Milad Qasemi
  • 3,011
  • 3
  • 13
  • 17
0

I managed to solve the issue. It might not be efficient but it does what I need.

I created 2 buttons in the same place. Those buttons are represented with different sprites (Pause & Play). "Pause" is visible since the start. When I click on it, the menu pops up, the "Pause" stop being active and the "Play" sprite button activates and pops up too. When I click on it, I unpause and goes back to the "Pause" sprite visible in the screen.

void Start () {
    Canvas_PauseMenu = Canvas_PauseMenu.GetComponent<Canvas> ();
    Button_Pause = Button_Pause.GetComponent<Button> ();
    Button_Resume = Button_Resume.GetComponent<Button> ();
    Canvas_PauseMenu.enabled = false;
    Button_Resume.enabled = false;
    Button_Resume.gameObject.SetActive (false);
}

// Update is called once per frame
public void PauseTest () {

    if(!active){
        PauseGame();
    }
    else{
        ResumeGame();
    }

}

public void BackToMainMenu()
{
    Application.LoadLevel (0);
}

public void PauseGame()
{
    Canvas_PauseMenu.enabled = true;
    Button_Exit.enabled = true;
    Button_Pause.enabled = false;
    Button_Pause.gameObject.SetActive (false);
    Button_Resume.enabled = true;
    Button_Resume.gameObject.SetActive (true);
    active = true;
    Time.timeScale = 0;
}

public void ResumeGame()
{
    Canvas_PauseMenu.enabled = false;
    Button_Exit.enabled = false;
    Button_Pause.enabled = true;
    Button_Pause.gameObject.SetActive (true);
    Button_Resume.enabled = false;
    Button_Resume.gameObject.SetActive (false);
    active = false;
    Time.timeScale = 1;
}
Sean Sabe
  • 79
  • 9