- Home /
Clearing a GUI and Pausing a Game
So i want to make that when first i move to this Scene, the game is paused. Until i press Start button, and all GUI cleared, and the game play again.. I've been searching all over the Answer section, yes i get a lot of good answer, but i can't done it..
So basically this is as far as i could do, and understand..
var images : Texture2D[];//5 images
var i : int;
function OnGUI () {
Time.timeScale= 0;
//fungsi untuk slide tutorial next dan previous
GUI.DrawTexture(Rect(200,50,100,100), images[i]);
if(GUI.Button(new Rect(210,300,100,50),"next texture")){
i++;
if(i>images.Length-1){i=4;}
GUI.DrawTexture(Rect(200,50,100,100), images[i]);
print(i);
}
if(GUI.Button(new Rect(100,300,100,50),"previous texture")){
i--;
if(i<images.Length - 5){i=0;}
GUI.DrawTexture(Rect(200,50,100,100), images[i]);
print(i);
}
if(GUI.Button(new Rect(400,400,50,50),"Start")){
//erase all GUI, but don't know how to do it
Time.timeScale=1.0;
}
}
i think using Time.timeScale is the best and easiest way(is that true?) to pause the game.. but when i press "Start" button, the time didn't go back to play.. i mean, anything still stop .. is there anyway to achieve this?
Answer by stevethorne · Jan 17, 2014 at 07:50 PM
Time.timeScale is a fine way of pausing the game. The issue you're having is that you're setting Time.timeScale to 0 every single call of OnGUI. You want to set it to 0 after a certain event. For example:
void Update()
{
if ( Input.GetKeyDown( KeyCode.Escape ) )
{
Time.timeScale = 0;
}
}
void OnGUI()
{
if ( Time.timeScale == 0 )
{
if ( GUI.Button( new Rect( 400, 400, 50, 50 ), "Start" ) )
Time.timeScale = 1.0;
}
}
This will check if you press escape and when you do, will show the gui to unpause it. Hope this helps to lead you in the right direction!
If you want the game to start paused you could move the code in Update() to your Start() function like this.
void Start()
{
Time.timeScale = 0;
}
void OnGUI()
{
if ( Time.timeScale == 0 )
{
if ( GUI.Button( new Rect( 400, 400, 50, 50 ), "Start" ) )
Time.timeScale = 1.0;
}
}
wow, it's working and i kind of understand about it now.. thanks @stevethorne could u hel me with "clearing GUI" things?
Your answer
Follow this Question
Related Questions
How Do I Center A GUI Label? 5 Answers
How to let the player save progress 1 Answer
Ensuring Correct Call Order 0 Answers
C# Script issue with Player 1 Answer
GUI_TEXTURE PROBLEM! 2 Answers