- Home /
Pause game without using timeScale
Hi all,
As the title suggests, is there are simple way to pause a game without using Time.timeScale
? I haven't decided on how I want the pause functionality will work in my game, but I can see problems I may run into if I use the "timeScale" method.
What are other ways to pausing a game?
Pls try to give examples in C#.
Thank you in advance for your help.
Answer by ninjapretzel · Jun 19, 2014 at 02:08 AM
With Unity 4.5, it is possible to use Time.timeScale, because there is also Time.unscaledDeltaTime and Time.unscaledTime for objects that don't need to get paused. This is probably the easiest solution, since you don't need to test for extra conditions like with the other solutions.
The other solutions would be to have a static 'paused' boolean somewhere (like make a static class Game, so it would be Game.paused), and either wrap all neccessary Update function code in an 'if (!Game.paused)', or add a behaviour to objects that disables other behaviours when Game.paused is true, and enables them when Game.paused is false.
Wow, really thank you. I was looking for a solution and I didn't know there is unscaledDeltaTime to use.
Answer by DangerPenguin · Jun 20, 2014 at 06:01 AM
NOTE:: My way of solving this should only work for simpler 2D games that don't heavily rely on the physics engine! Use at your own risk, ymmv.
I usually solve this with a global variable(or a singleton) for whether the game is paused or not, and then in any object the uses its update function to "act", I check if the game is paused before I let it do anything.
Here's some Code.
You need to make a script to hold all your global variables, (mine is GlobalVariables.js) and attach it to some object that will always be in your scene (like an empty object JUST for this).
#pragma strict
static var isPaused : boolean;
function Start () {
isPaused = false;
}
Attach this to the empty object in your scene.
Now, inside of ANY object that has to "act", change its update function to look like this.
function Update () {
if(!GlobalVariables.isPaused ){
//All your old code/update code goes in here!
}
}
And voila, your game doesn't do anything when you set GlobalVariables.isPaused to true, but anything inside of OnMouseDown functions will still fire!
EDIT:: fixing words
Answer by tapir · Jun 19, 2014 at 02:06 AM
You can use events for that. You can create an event manager that will call the function Pause() then you click a button. In your scripts you can create a function OnPause() and suscribe it to the event manager. Inside the OnPause() function you can handle the pausing logic for each script.
This is a great tutorial for events! Hope it helps.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
what is wrong whis this? 1 Answer
Where can I find C# tutorials for a absolute beginner? 7 Answers
Crash the entire game 3 Answers