I have a pause menu script but it still doesn’t work for me. So does anybody have a pause menu script that I can use.
I imagine someone might post a script for you (there’s a couple such scripts floating around), but, you could also post your script for us to look at (along with a description of how it’s not working).
Okay, I must warn you: IT IS A PRETTY LONG PIECE OF CODE. Now, here is the problem. It doesn’t pause when I press escape.
var skin:GUISkin;
private var gldepth = -0.5;
private var startTime = 0.1;
var mat:Material;
private var tris = 0;
private var verts = 0;
private var savedTimeScale:float;
private var pauseFilter;
private var showfps:boolean;
private var showtris:boolean;
private var showvtx:boolean;
private var showfpsgraph:boolean;
var MenuMusic : AudioClip;
var lowFPSColor = Color.red;
var highFPSColor = Color.green;
var backdrop : Texture2D; // our backdrop image goes in here.
var lowFPS = 25;
var highFPS = 50;
var start:GameObject;
var url = "unity.html";
var statColor:Color = Color.yellow;
var credits:String[]=["Credits would go here"] ;
var crediticons:Texture[];
enum Page {
None,Main,Options,Credits
}
private var currentPage:Page;
private var fpsarray:int[];
private var fps:float;
function Start(){
Time.timeScale = 1;
Screen.showCursor = false; //(hides the cursor)
}
function OnPostRender() {
if (showfpsgraph mat != null) {
GL.PushMatrix ();
GL.LoadPixelMatrix();
for (var i = 0; i < mat.passCount; ++i)
{
mat.SetPass(i);
GL.Begin( GL.LINES );
for (var x=0; x<fpsarray.length; ++x) {
GL.Vertex3(x,fpsarray[x],gldepth);
}
GL.End();
}
GL.PopMatrix();
ScrollFPS();
}
}
function ScrollFPS() {
for (var x=1; x<fpsarray.length; ++x) {
fpsarray[x-1]=fpsarray[x];
}
if (fps < 1000) {
fpsarray[fpsarray.length-1]=fps;
}
}
static function IsDashboard() {
return Application.platform == RuntimePlatform.OSXDashboardPlayer;
}
static function IsBrowser() {
return (Application.platform == RuntimePlatform.WindowsWebPlayer ||
Application.platform == RuntimePlatform.OSXWebPlayer);
}
function LateUpdate () {
if (Input.GetKeyDown("escape") Pause) {
audio.Stop();
}
if (Input.GetKeyDown("escape")) {
switch (currentPage) {
case Page.None: SetPause(); break;
case Page.Main: if (!IsBeginning()) UnSetPause(); break;
default: currentPage = Page.Main;
}
}
}
function OnGUI () {
if(currentPage == Page.Options){
var backgroundStyle : GUIStyle = new GUIStyle();
backgroundStyle.normal.background = backdrop;
GUI.Label ( Rect( (Screen.width - (Screen.height * 2))* 0.75, 0, Screen.height*2,Screen.height), "" , backgroundStyle);
}
if (skin != null) {
GUI.skin = skin;
}
if (IsGamePaused()) {
// GUI.color = statColor;
switch (currentPage) {
case Page.Main: Pause(); break;
case Page.Options: ShowToolbar(); break;
}
}
}
private var toolbarInt:int=0;
private var toolbarStrings: String[]= ["Audio","Graphics"];
function ShowToolbar() {
BeginPage(450,300);
toolbarInt = GUILayout.Toolbar (toolbarInt, toolbarStrings);
switch (toolbarInt) {
case 0: VolumeControl(); break;
case 1: Qualities(); QualityControl(); break;
}
EndPage();
}
function ShowCredits() {
BeginPage(300,250);
for (var credit in credits) {
GUILayout.Label(credit);
}
for (var credit in crediticons) {
GUILayout.Label(credit);
}
EndPage();
}
function ShowBackButton() {
if (GUI.Button(Rect(20,Screen.height-50,80,40),"Back")) {
currentPage = Page.Main;
}
}
function Qualities() {
switch (QualitySettings.currentLevel) {
case QualityLevel.Fastest:
GUILayout.Label("");
GUILayout.Label("Lowest");
GUILayout.Label("");
break;
case QualityLevel.Fast:
GUILayout.Label("");
GUILayout.Label("Low");
GUILayout.Label("");
break;
case QualityLevel.Simple:
GUILayout.Label("");
GUILayout.Label("Middle");
GUILayout.Label("");
break;
case QualityLevel.Good:
GUILayout.Label("");
GUILayout.Label("Higher");
GUILayout.Label("");
break;
case QualityLevel.Beautiful:
GUILayout.Label("");
GUILayout.Label("Highest");
GUILayout.Label("");
break;
case QualityLevel.Fantastic:
GUILayout.Label("");
GUILayout.Label("Ultra");
GUILayout.Label("");
break;
}
}
function QualityControl() {
GUILayout.BeginHorizontal();
if (GUILayout.Button("Decrease")) {
QualitySettings.DecreaseLevel();
}
if (GUILayout.Button("Increase")) {
QualitySettings.IncreaseLevel();
}
GUILayout.EndHorizontal();
}
function VolumeControl() {
GUILayout.Label("");
GUILayout.Label("Master Volume");
GUILayout.Label("");
AudioListener.volume = GUILayout.HorizontalSlider(AudioListener.volume,0.0,1.0);
}
function BeginPage(width,height) {
GUILayout.BeginArea(Rect((Screen.width-width)/2,(Screen.height-height)/2,width,height));
}
function EndPage() {
GUILayout.EndArea();
if (currentPage != Page.Main) {
ShowBackButton();
}
}
function IsBeginning() {
return Time.time < startTime;
}
function Pause() {
audio.PlayOneShot(MenuMusic, 1.0 / audio.volume);
Time.timeScale = 0;
var backgroundStyle : GUIStyle = new GUIStyle();
backgroundStyle.normal.background = backdrop;
GUI.Label ( Rect( (Screen.width - (Screen.height * 2))* 0.75, 0, Screen.height*2,Screen.height), "" , backgroundStyle);
Screen.showCursor = true; //(shows the cursor)
if (Input.GetKeyDown("escape")) {
Screen.showCursor = false; //(hides the cursor)
}
BeginPage(300,300);
if (GUILayout.Button (IsBeginning() ? "Resume" : "Resume")) {
UnSetPause();
Screen.showCursor = false; //(hides the cursor)
audio.Stop();
}
if (GUILayout.Button ("Options")) {
currentPage = Page.Options;
}
if (GUILayout.Button ("Quit To Main Menu")) {
Application.LoadLevel("MainMenu");
}
if (GUILayout.Button ("Quit To Windows")) {
Application.Quit();
}
if (IsBrowser() !IsBeginning() GUILayout.Button ("Restart")) {
Application.OpenURL(url);
}
EndPage();
}
function SetPause() {
savedTimeScale = Time.timeScale;
Time.timeScale = 0;
AudioListener.pause = true;
if (pauseFilter) pauseFilter.enabled = true;
currentPage = Page.Main;
}
function SetPause (pause : boolean)
{
Input.ResetInputAxes();
var gos : Object[] = FindObjectsOfType(GameObject);
for (var go : GameObject in gos)
go.SendMessage("DidPause", pause, SendMessageOptions.DontRequireReceiver);
transform.position = Vector3.zero;
if (pause)
{
Time.timeScale = 0;
transform.position = Vector3 (.5, .5, 0);
guiText.anchor = TextAnchor.MiddleCenter;
}
else
{
guiText.anchor = TextAnchor.UpperLeft;
transform.position = Vector3(0, 1, 0);
Time.timeScale = 1;
}
}
function UnSetPause() {
Time.timeScale = savedTimeScale;
AudioListener.pause = false;
if (pauseFilter) pauseFilter.enabled = false;
currentPage = Page.None;
if (IsBeginning() start != null) {
start.active = true;
}
}
function IsGamePaused() {
return Time.timeScale==0;
}
function OnApplicationPause(pause:boolean) {
if (IsGamePaused()) {
AudioListener.pause = true;
}
}
function OnMouseDown ()
{
Screen.showCursor = false; //(hides the cursor)
}
Pause();
if (Input.GetKeyDown("escape")) {
// code here
}
Yeah, that’s going to be a little hard to sort through, especially since there’s no indentation. (I’m not sure exactly why indentation isn’t preserved in some cases, but I always convert tabs to spaces before posting code, and that always seems to work.)
Well I was told that I need to have this piece of code:
Code:
if (Input.GetKeyDown(“escape”)) {
// code here
}
Or call the pause function again here:
Pause();
or set the timescale to 0 again (as this pauses the game)
Time.timeScale = 0.0;
Hi,
depending on how complex your game is, you can stop the player (and the enemies, obstacles, etc.) object and then instantiate your GUI?
Yeah, I’m not so good with code. I can do simple code but not that complex and when I got it it didn’t have any indentations either.
I can see at least one problem, referencing an undefined Pause variable when you’re checking for the escape key, (or maybe you intended to call Pause(), but in any case that script seems to be a slightly mangled version of this, so you might want to start with the original:
http://www.unifycommunity.com/wiki/index.php?title=PauseMenu
Hey i have made a MenuPackage and it does have a pauseGame script. You can find the thread here:
The post also contains the decription about the package.
Ofcourse you can modify the script according to your needs. Hope it may help you.
Edit:My scripts include the implementation of TimeScale and it also stops Update function to process while in pause mode. Not only this you can even switch off/on the music and it also remembers the last settings set by the user and loads them whenever any scene loads.
if tou make sure that all movement in you game is moved with Time.deltaTime, you just have to make a button wich toggles between:
Time.timeScale = 0;
and
Time.timeScale = 1;