- Home /
Pause menu
Attached to my player i have a pause menu script wich looks like this
var paused : boolean = false; var myCheck : boolean = false;
function Update () { if(Input.GetButtonDown( "Pause" ) ) { if(!paused) { Time.timeScale = 0; paused = true; }else{ Time.timeScale = 1; paused = false; } } }
function OnGUI() { if( paused ) { if(GUI.Button (Rect ( 10, 50, 100, 30), "Resume" )) { Time.timeScale = 1.0F; paused = false; } if(GUI.Button (Rect ( 10, 90, 100, 30), "Restart" )) { Application.LoadLevel( "stage1" ); }
if(GUI.Button (Rect ( 10, 130, 100, 30), "Options" )) { } if(GUI.Button (Rect ( 10, 170, 100, 30), "Quit" )) { } } }
it is functional and appear/disappear upon pressing the pause button (P) but the problem is, when i press the "test game" button in unity, the game starts with the pause menu showing on screen. i can move around like normal but the pause buttons are still showing on screen and they are functional when pressed. if i press the pause button (P) the menu goes away until i either reload the mission by dying or press the pause button. can i get a version of this script where this problem is not present?
Answer by fafase · Sep 29, 2012 at 05:59 PM
Try to add:
function Start(){
paused=false;
}
Every time you will start the scene then the variable is set. You also need to reset Time.timeScale to 1 when pressing Restart. I would think that at the moment, restarting reload the scene but nothing moves.
okay, so i added the text u suggested (at the top, the line above "function update" and below var paused : boolean = false; var myCheck : boolean = false;) and my original problem was solved. the game launched the way it was supposed to, but another problem emerged. when i now, using the new script, pressed the restart button in pause menu, the game sent me back to the level, but i was unable to move. the game was infact in pause mode, but the GUI buttons where not showing. in order to resume play i had to open and the close the pause menu. any sugestions?
If you read my answer a second time, you will see that I answered your second problem already:).
You also need to reset Time.timeScale to 1 when pressing Restart. I would think that at the moment, restarting reload the scene but nothing moves.
timeScale is a static variable meaning it survives from one scene to another and so does its state. You need to add:
if(GUI.Button (Rect ( 10, 90, 100, 30), "Restart" )) {
Time.timeScale = 1.0f;
Application.LoadLevel( "stage1" );
THAN YOU SOOOO $$anonymous$$UCH! u just solved my problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081642im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Score wont stop after game is paused 3 Answers
When i press play it just pauses right after. 6 Answers
Make Time.timeScale not effect pause menu? 1 Answer
Stop Whole game except one one object 1 Answer
Problems with "Overhead" 1 Answer