- Home /
Making a pause Menu
General question, I have seen quite alot of people asking if there can be a way to make a Pause screen, Then i have seen people answer with the answer create a new scene and GUI buttons and Things on to it But it wouldn't work because it would reset the Game right??,, my question is Couldn't i just make a GUI texture that That activates when i press like the button "M" or something to bring up the GUITexture with like a map and a little red dot to show where i am ??,, then press "M" again to deactivate it :/ Sorry if this question doesn't make sense but it did in my head :P
That is the right way of doing it. You were properly thinking about the main menu. For a main menu the(most correct) way would properly be to create a new scene, but for a pause menu you would just set the Time.timeScale to zero and add some GUIs on the screen ;)
Answer by nastasache · Nov 17, 2011 at 11:06 PM
Short answer: change the name of the 'PauseMenu' function both in line 29 and 24.
Long answer: If you are not a coder at all (not so difficult to become one) I will try to learn you how to deal with the errors: when you have an error, pay attention to the most important information: line and character position where error was. For example "(Line 29,10)" mean that there is a problem at line 29 on the script, starting with character number 10, aka 'P'. Look for the string started there with 'P'. This is 'PauseMenu'. So that it's something strange with this string. The probably you must to change it. But where? (there is 3 occurrences of 'PauseMenu' in the script: first one at line 4; second one at line 24 and last one at line 29 where you have the error). The tip is to look what the 'PauseMenu' mean at line 29. It's a function, because have '()' along. Where this function it's called / used? Here you must to investigate a bit what 'PauseMenu' mean at line 4 and at line 24. At line 4 doesn't look like a function; it's a name of the object; so probably you don't want to change this (you maybe use this as name to refer it from other scripts). But in line 24? There looks as parameter of another function - named "GUILayout.Window ()", respectively the paramenter #3. Then, since the "GUILayout" it's an Unity thing, go to Unity documentation and search for "GUILayout.Window" (http://unity3d.com/support/documentation/ScriptReference/GUILayout.Window.html). You will discover there a line:
static function Window (id : int, screenRect : Rect, func : GUI.WindowFunction, text : String, : ) : Rect
The function name is 'Window'; The #3 parameter is "func : GUI.WindowFunction" (func=it's a function). Now, you have the confirmation: 'PauseMenu' on line 24 it's name of a function. What this mean? Mean that on line 24, you call the function defined at line 29. And looks like the engine don't like you use the current name of the function. Just change it name, in both places: where function are declared; and where function are called (for example, to 'ShowPauseMenu'):
Line 24: GUILayout.Window(0, windowRect, ShowPauseMenu,
Line 29: void ShowPauseMenu(int windowPause) {
Hey man cheers for taking the time to explain errors and stuff to me should make it a hell of a lot simpler now :)
Answer by nastasache · Nov 13, 2011 at 02:58 PM
It would not reset the Game if you use an object with DontDestroyOnLoad() and use this object during of game play:
Script: Pause.js attached to a Pause object in Pause scene:
static var gamePaused : boolean = false;
function Awake() {
DontDestroyOnLoad(this);
}
function Start() {
Application.LoadLevel("Level");
}
function OnGUI () {
if(gamePaused) {
GUILayout.Label ("GUI buttons here");
}
}
function LateUpdate () {
if(Input.GetKeyUp(KeyCode.M)){
if(gamePaused) {
Time.timeScale = 1.0;
gamePaused = false;
} else {
Time.timeScale = 0.0;
gamePaused = true;
}
}
Debug.Log("gamePaused = " + gamePaused);
}
Script: Level.js attached to a Level object in Level scene:
function Start () {
gameState = FindObjectOfType(Pause);
gameState.gamePaused = false;
}
function Update () {
Debug.Log ("Level is running, Time.time is = " + Time.time);
}
Add Level scene to the Build Settings, start the game with Pause scene and watch the log.
Sorry, Wentzel, I did not want to overlap the efforts. I thought you finished the post and I just completed how to pause without resseting game. Please post your version if you work on. I am newbie in Unity and I have to learn also if more answers here. Thanks.
No i meant it like a joke lol. Your solution works better :P
Answer by vozochris_1 · Nov 13, 2011 at 05:42 PM
Why don't you make buttons in a Window instead of just buttons? Well, I don't know javascript, I will write in C#.
using UnityEngine; using System.Collections;
public class PauseMenu : MonoBehaviour {
public Rect windowRect = new Rect(295,175,0,0);
public bool gamePaused = false;
void Update(){
if(Input.GetKeyUp(KeyCode.M)){
if(gamePaused){
gamePaused = false;
Time.timeScale = 1.0f;
} else {
gamePaused = true;
Time.timeScale = 0.0f;}
}
}
void OnGUI(){
if (gamePaused){
Time.timeScale = 0.0f;
GUILayout.Window(0, windowRect, PauseMenu,
"Game Paused", GUILayout.Width(100));
}
}
void PauseMenu(int windowPause) {
if (GUILayout.Button("Resume")){
Time.timeScale = 1.0f;
gamePaused = false;
}
}
}
It will make a window with name "Game Paused" and 1 button "Resume" in window appear. You can either press M either "Resume" button to continue. You can Add any GUILayout's in window at "void PauseMenu(int windowPause)".
Errors? There are no errors, I tested it :| ... by the way it's C# not javascript. And I use this, same code in my current game which is under development, and it works fine
Well it gives me two errors When i use it this first one is Assets/Pause$$anonymous$$enu.cs(30,6): error CS0542: `Pause$$anonymous$$enu.Pause$$anonymous$$enu(int)': member names cannot be the same as their enclosing type
and the second one is Assets/Pause$$anonymous$$enu.cs(41,1): error CS8025: Parsing error ??
The parsing error is because of a missing "}" before the OnGUI() function. The other error, the compiller has already given the cause: change the name of Pause$$anonymous$$enu function to something else and it will work.
I keep getting this error Assets/Scripts/Pause$$anonymous$$enu.cs(29,10): error CS0542: `Pause$$anonymous$$enu.Pause$$anonymous$$enu(int)': member names cannot be the same as their enclosing type
Answer by syclamoth · Nov 13, 2011 at 01:35 PM
No, there is absolutely no reason why you can't just draw some gui elements on the screen, like a minimap and some buttons. That's what I do, and it works just fine! I don't know about all this talk of making new scenes full of gui elements- as you say, that would reset the game (and you don't want that- it's a pause button not a reset button)
Answer by Wentzel · Nov 13, 2011 at 02:24 PM
As @ OrangeLightning said : Time.timeScale would work fine.
var paused : boolean = false;
function Update () {
if(Input.GetKeyUp(KeyCode.M)){ if(!Paused){ Time.timeScale = 0; paused = true; } } }
As for the GUI buttons.
var MyTexture : Texture;
function OnGUI(){ GUI.Box(Rect(250,50,300,400),"Menu");
if(GUI.Button(Rect(360,100,80,20),"Resume")){ PauseScript.paused = false }
if(GUI.Button(Rect(360,130,80,20),"Quit")){ Application.Quit(); } }
Your answer
Follow this Question
Related Questions
Way to have a pause and play texture in same GUI texture? 0 Answers
GUI.DrawTexture not working? (nothing shows up) 1 Answer
Touchscreen for unity 2 Answers
water mark texture 0 Answers
Wearied Error 1 Answer