I have a button and I want to use it for pause and unpause the menu when I click on it. ¿How I can do it? I appreciate your help.
Hey there SeanSabe!
If you just want to pause your game, you will want to set the time scale too zero.
To do this, you need to make a script, and somewhere in the script create a public method with this bit of code in it (C#):
Time.timescale = 0f
Once this is done, you will need to call the method when you click the button.
I would suggest you watch this video to learn how to do this if you do not know already:
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
Of course, you will also wish to unpause your game too. ![]()
So I suggest creating a float variable that you store the starting timescale in before you set the scale to zero.
Once you have the original timescale safely stored, you then call the Time.timescale function above, but instead of setting it too zero, you set it to the variable holding the starting timescale.
Hope this helps! ![]()
~TB
Thanks for the answer. Actually that’s what I did. It might not be pretty efficient but it does what I need.
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;
}