- Home /
pause game
Help!!!
I want to pause my game when after i click the mouse. i can't get it right.
Here's my code:
pragma strict
private var guiOn = false;
var wasClicked : boolean;
var paused : boolean = false;
function OnMouseDown()
{
wasClicked = true;
OnPausedGame();
Activate();
}
function OnMouseEnter()
{
if (wasClicked)
{
Activate();
}
}
function Activate()
{
guiOn = true;
OnPausedGame();
//OnGUI();
}
function OnGUI()
{
if (guiOn)
GUI.Box(new Rect(0, Screen.height - 50, 300, 100), "test");
}
function OnPausedGame()
{
if (wasClicked)
{
if (!paused)
{
Time. timeScale = 0;
paused = true;
OnGUI();
}
}
}
When you say "can't get it right", what is actually wrong?
i don't know how to arrange the code that will pause the game. i dont know where to place OnPausedGame to the other functions.
Answer by jong · Feb 02, 2013 at 05:52 PM
Can I ask another question?
How can i put an if statement to GUI.button?
Of course.
function OnGUI()
{
if(GUI.Button(Rect(10,10,150,100), "Pause/UnPause"))
{
if(Time.timeScale == 1)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
}
Answer by Doireth · Feb 02, 2013 at 10:23 AM
To pause a game you could simply write
function Update()
{
if(Input.GetButtonDown("Fire1") && Time.timeScale != 0)
{
Time.timeScale = 0;
}
else
{
Time.timeScale = 1;
}
}
This is a basic example of pausing. Your code seems somewhat excessive but then again I don't know how your program works.
Can i separate this part?
else
{
Time.timeScale = 1;
}
I want to put it under GUI.button. so when player clicks the button, the game resumes.
sorry i'm still newbie to unity and javascript.
Sure, that's perfectly valid (though you won't use the "else" part if it's no longer part of an if/else statement. Just the "Time.timeScale = 1" bit).
$$anonymous$$y code was merely an example of pausing. It was only meant to illustrate the idea, you should adapt it to your needs.
Your answer
Follow this Question
Related Questions
How to Disable/Inactive NGUI Button on Game Pause? 0 Answers
Can you pause unity playmode while mouse button is down? 2 Answers
C# moveOnMouseClick Rotation 2 Answers
Play a video ingame by clickin on gameobject 3 Answers
Camera Orbit on Left Click problem 0 Answers