- Home /
How do I design a pause function?
I'm new to Unity, I'm trying to create a game which requires a pause function.The Pause should work as; when player click the pause button the game should stop its function and a window/image should pop up with 2 options, main menu and resume. If the player click main menu the main menu scene should appear, if resume the pop up window should disappear and the game should continue in the same scene. How do I have to write the script for that?
Put down your words into a more appropriate situation:
if(pause key is press) enable pause, stop game
if (pause enabled)show gui, drawing 2 buttons
if(GUI button for resume is pressed) disable pause, restart game
if(GUI button for main menu pressed) disable pause, restart game, load menu
This is pretty much a pseudo code of your Pause script.
Also, I am pretty sure there are already a few answers on this topic.
Thank you for the help, I used the below code but it doesn't work as I wish. the problem is before I press pause button the GUI window appears, what is wrong with this code?
pragma strict
var isPause = false; var $$anonymous$$ain$$anonymous$$enu : Rect = Rect(10, 10, 200, 200); var WinTexture : Texture; function Update () { if( Input.Get$$anonymous$$ouseButton(1)) { isPause = !isPause; if(isPause) Time.timeScale = 0; else Time.timeScale = 1; } }
function OnGUI()
{
GUI.Button (new Rect (230, 540, 100, 50), "Pause");
isPause = !isPause;
if(isPause)
GUI.Window(0, $$anonymous$$ain$$anonymous$$enu, The$$anonymous$$ain$$anonymous$$enu, WinTexture);
}
function The$$anonymous$$ain$$anonymous$$enu () {
if(GUILayout.Button("$$anonymous$$ain $$anonymous$$enu")){
Application.LoadLevel("$$anonymous$$ain$$anonymous$$enu");
}
if(GUILayout.Button("Restart")){
Application.LoadLevel("GamePlay");
}
}
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. At Unity Answers, Answer means Solution, not Response.
Please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Answer by VIPINSIRWANI · May 05, 2014 at 07:20 AM
Hi silo, For game pause action here some steps please follow these steps.
public GameObject PopUp;
First make one pop up window with Resume or Restart buttons(Gui Textures)
In Start() false that window. Ex = PopUp.SetActive(False);
On Pause Click true that window Ex = PopUp.SetActive(true);
For Game Functionality pause you can use Time.timeScale = 0 for pause and time.TimeScale = 1 for game resume but this is not good practice to play with frames call so instead of that thing you can use some boolean value for Pause and Resume.
On click Resume just disable popup and resume your game window and on restart just reload your game.