I am trying to use either the menu button or the back button to pause the game, and then use eitherthe back button again or a resume button to continue playing, but only the resume button is working and not the back button, I think its pausing and resuming in the same frame so I need to put in a wait, here is what I have:
#pragma strict
//Set variables
var btnWidth:int = 225;
var guiMode:String = "InGame";
var gameActive:boolean = true;
//Update function ran every frame
function Update () {
//If the game is not paused
if(gameActive)
{
//If the menu or escape button is pressed
if(Input.GetKeyUp(KeyCode.Menu) || Input.GetKeyUp(KeyCode.Escape))
{
//Pause the game
guiMode = "Pause";
gameActive = false;
Time.timeScale = 0;
}
}
}
//Display GUI items
function OnGUI ()
{
//If pause mode is set
if(guiMode =="Pause")
{
//If the resume or back button is pressed resume gameplay
if(GUI.Button(Rect(Screen.width/2-(btnWidth/2),Screen.height/2-70,btnWidth,60), "Resume Game") || Input.GetKeyUp(KeyCode.Escape))
{
Time.timeScale = 1;
guiMode = "InGame";
gameActive = true;
}
}
}
I tried adding in a yield WaitForSeconds(0.5) command but only got a “can not be a coroutine” error
//If the resume or back button is pressed resume gameplay
if(GUI.Button(Rect(Screen.width/2-(btnWidth/2),Screen.height/2-70,btnWidth,60), "Resume Game") || Input.GetKeyUp(KeyCode.Escape))
{
yield WaitForSeconds(0.5);
Time.timeScale = 1;
guiMode = "InGame";
gameActive = true;
}
I figured out where I went wrong, here is what I now have if anyone else needs a solution:
#pragma strict
//Define variables
var btnWidth:int = 225;
var guiMode:String = "InGame";
var gameActive:boolean = true;
//Update function ran every frame
function Update () {
//If the game is not paused
if(gameActive)
{
//If the menu or escape button is pressed pause the game
if(Input.GetKeyUp(KeyCode.Menu) || Input.GetKeyUp(KeyCode.Escape))
{
guiMode = "Pause";
gameActive = false;
Time.timeScale = 0;
}
}
//If the game is paused
else if(!gameActive)
{
//If the menu or escape button is pressed resume the game
if(Input.GetKeyUp(KeyCode.Menu) || Input.GetKeyUp(KeyCode.Escape))
{
guiMode = "InGame";
gameActive = true;
Time.timeScale = 1;
}
}
}
//Display GUI items
function OnGUI ()
{
//Display PAUSE menu
if(guiMode == "Pause" !gameActive)
{
//If the resume button is pressed resume gameplay
if(GUI.Button(Rect(Screen.width/2-(btnWidth/2),Screen.height/2-70,btnWidth,60), "Resume Game"))
{
guiMode = "InGame";
gameActive = true;
Time.timeScale = 1;
}
//If restart level button is pressed restart the level
if(GUI.Button(Rect(Screen.width/2-(btnWidth/2),Screen.height/2,btnWidth,60), "Restart Level"))
{
Time.timeScale = 1;
Application.LoadLevel(Application.loadedLevel);
}
//If the main menu button is pressed, return to the main menu
if(GUI.Button(Rect(Screen.width/2-(btnWidth/2),Screen.height/2+70,btnWidth,60), "Main Menu"))
{
Time.timeScale = 1;
Application.LoadLevel(0);
}
}
}