- Home /
How to make a pause function?
How can I make a pause function that when a pause button is pressed, the game stops.
Do I really need to disable everything from every game object or is there an easier way to just stop everything?
Seriously, use the search function. It would've been less trouble to look this up then to type your question..
Here is a tutorial that $$anonymous$$ches you how to do that: http://youtu.be/z5oA-DvVGoo
Answer by FreeTimeDev · Mar 17, 2011 at 02:57 PM
var isPaused : boolean = false;
function Update() { if(Input.GetKeyDown("p")) { Pause(); } }
When the P key is pressed it will try to run the Pause function. So, make a pause function.
function Pause()
{
if (isPaused == true)
{
Time.timeScale = 1;
isPaused = false;
}
else
{
Time.timeScale = 0;
isPaused = true;
}
That, i think, should work. Not tested or anything but the worst that can happen is that someone else will correct me =)
this one is simple, and useful good work matthew thumbing you up
Thanks poncho. I do want to point out that it'd be really easy to have "things happen" in the Pause function. and you can have other scripts check if the game is paused by checking isPaused (if you make the variable static). Pretty versatile, i think.
Answer by FLASHDENMARK · Mar 02, 2011 at 09:21 PM
Fx:
function Update ()
{
if(Input.GetKeyDown("p"))
{
Time.timeScale = 0;
}
}
When the "P" key is pressed the game pauses. When you want to "Unpause" it just use:
Time.timeScale = 1;
Hope this helped :)
how do you add in the unpause part im confused
Answer by Coreyf716 · Oct 21, 2012 at 02:18 AM
Try using this.
function Update () {
if (Input.GetKeyDown(KeyCode.P)) {
isPaused = !isPaused;
}
Time.timeScale = isPaused ? 0 : 1;
}
cool, but what about pause the game sounds?, $$anonymous$$e are still playing