I just recently added a pause option to my game for the first time and I also did it by setting Time.timeScale = 0 and then having a static bool that kept track of if the game was paused or not. I find doing it this way is pretty easy and intuitive. Also did not know that you can just pause all AudioSources just from one line of code lol, thanks for bringing this up!
The same thing could not function properly on the WebGL; both Ignore Time Scale and Remember Read Positions were enabled and the music suddenly went rapidly off while the audio is paused.
The quality of this video is insane, very well scripted and explained, great editing and the information in it is really different from what would you expect from similar videos explaining the same topic, when the video said: "the right way" something in my head told me: this guy knows what he is doing👏 really great content, I feel that we lost brackeys, but we are going to have this guy. good luck man, really great content.
Hey, dude, you won't believe the immense help you provided me with! I can't stress enough how grateful I am. Remember when you mentioned that crucial detail at 2:08? Well, I was in the middle of creating the Pause Menu for my game and encountered a perplexing issue. Whenever I transitioned back to the Game Scene from Main Menu, the game froze completely. I tried everything, even deep profiling, but couldn't make any sense of it. All I could observe was that the 'asyncoperationtocomplete()' function was taking an unusually long time. However, thanks to your advice, I decided to set the timescale to 1 before exiting the Game scene, and voila! It miraculously worked! I cannot thank you enough for your invaluable assistance!💕💕
Never quit uploading content, your video quality is really a rare gem to find in the Unity tutorial world these days. This video truly felt like being in a university actually learning something. Keep up the great work.
I recently had issues in my game with using TimeScale at 0 to pause, as it can potentially stop other scripts in the game midway through execution. The way I got around this was to have the pause button set a bool, then run the check to set the Timescale to 0 in LateUpdate() as this is more likely to run once everything else is finished in that frame.
2:21 very nitpicky catch here, but deltaTime is actually the time between the last frame and the frame before it, not the time between the last and current frame. This is because when deltaTime is called, the program doesn't know when the current frame is going to appear, so deltaTime is a close approximation of how long the current frame will *probably* take to render. Jonas Tyroller did a great video about this, in case you haven't watched it. Given that this video was released over a year ago, I trust that you've learned a lot more and will have fewer similar errors in your course. I preordered it and am very much looking forward to its release!
No. It's the time between the last frame and the current one (see the Unity docs). In other words, deltaTime is how long the last frame lasted. Of course it would be better if deltaTime was exactly equal to the duration of the current frame, but you would need a time machine for that.
I'm using the new Input System which fires an event when P is pressed. This flips a boolean flag property: it's true, run the Pause logic. If its now become false, run the Unpause logic. Note that this is a C# getter/setter property, not just a boolean variable on its own. The pause logic sets Time.timeScale to 0 which effectively freezes the main game state, but also uses unscaled time to allow certain other things to carry on (e.g. animations on the pause screen itself). It also uses the Input System to disable/enable certain Actions, to in effect "lock out" certain controls while the game is paused, i.e. the gameplay controls are locked out while the Pause and Quit buttons still work. This prevents the game from going utterly haywire once unpaused again. I'm convinced my method is absolute crap and fully expect to stumble across a much better and cleaner way at some point.
I had just had a similar problem and this is the video that I would have liked to see first. It has a very good quality, it focuses on explaining the deep instead of the superficial and about the article, the article is INSANE, well explained and looks great. you gained a spanish speaking subscriber
This was really helpful. I've traditionally just been using a GameState object that sends out a 'GamePaused' event, and having all of the objects that need to respond to that and set their own 'isPaused' state accordingly, but things with Physics of course don't follow that so that's been a challenge. I totally didn't know you could do this, this helps so much, thank you! Love your blog too!
Thanks for this. It helped me solve the problem of letting a user type into an input field and not have the navigation actions take place if you typed an E or other nav key.
to be fair, this would generally apply to most things, as the majority of engines or IMEs that have deltaTime defined will generally also define timescale and unscaledDeltaTime, its pretty standard
I just found your channel, I love your content and your way of explaining things. I just watched the Unity events Video and was impressed so I supped and started watching your other videos. continue with this quality and you'll have 500k sups in no time. good luck!
This video was absolutely brilliant in how succinct and well-explained it was. The channel deserves way more subscribers. Out of curiosity - which IDE are you using here? I couldn't work it out.
First of all, awesome video, thanks a ton for sharing this! Personally I don't modify the timescale at all, but rather keep a collection of tracked objects that are subscribed to the pause event which pause/unpause their behavior.
this a great solution to my the problem i have because like you say time scale effects only the time the have a lot off issues like for example wean you make objects with bool and the are set active or the opiset with having a "is pause" bool or something similar you easy able to make the game better because you able to disable and enable stern factions like game inputs for example and other stuff you don't wond to happen one other good ideas is to find some way to disabled some off thos faction event the game is not pause for example if someone make i multiplayer game for example it's good idea to make thos system to stop event if not on the pase the game for example if you play a multiplayer you don't wand to puse the game wean you press the menu but you still wand to disable the player input wean you press this button
Do you mean when the controller's unplugged? The Rewired plugin, as an example, has a disconnect event, that you could subscribe a pause function to. There may well be a native version of that, I would've thought the new Input System has something along those lines too.
As obvious as it reads(hehe) you can also pause by disabling movement scrips storing vector values, (dont forget about gravity). Also the way shown in the video is better by any margin of imagination.
i stopped game with time.timescale=0; but i cant work this code "Button.interactable=true;" i searched and i probably i need to solve this with time.unscaledTime but how can i do it? help me plz
Thanks! Do you mean @08:18? It checks if the game is already paused and then changes it to the opposite state, which is why it probably looks the wrong way round.
Excellent video! I tried using timeScale=0 and it works great in editor, but in a build Windows says the app is unresponsive after being paused for 15-30 seconds, basically crashing the game. I read something about setting it to 0.001f instead, but that didn’t change it. Do you have any idea on where to look for any code that could cause this to happen? Thanks!
I hadn't heard of that but I'm going to look into it. It's possible that something else is happening, so maybe try pausing a build of a blank project to double-check.
I was hoping to find a video on scene management. I have scene1 which on a button click uses a function to load scene2. In the OnSceneLoaded, I check if the scenename is Scene2 and if it is , I tried Gameobject.Find to find a text field in my scene to put something into it. But that line of code is dying, getting a NullReferenceException : Object reference not set to an instance of an object" error. How do I make sure the scene is completely loaded, i.e. all gameobjects on it have been instantiated before I try and Find them? Your help would be most appreciated.😀
Thanks for the tutorial. I've been having a problem with the pause function in my game: i am doing a game about protecting a cell in the middle of the screen by clicking on targets and dragging others for points, i applied the pause script and it works in stopping time but doesn't prevent interactions with the enemies and the point tokens, they can be dragged and removed, all the tutorial talk about pause time but it doesnt' work when my game doesn't use it or the only thing using time are the enemies slowly going toward the center. Tried 2d and 3d sprites, expecting it was a thing related to it but didn't. Any help you or anyoen can provide, please?
So my thinking would be that the type of interaction you're talking about is, essentially, input, which because it's not scaled by time in any way, you would need to turn it off yourself. Maybe using a global event in whatever script you're using to trigger and detect those clicks.
Been looking into some threads and videos, none managed to answer my question... You see, i got 3 methods, PauseMenu, Inventory Menu and GameOverScreen, all of then use the same structure as this: public void PauseMenu() { if(Input.GetButtonDown("Pause") && isMenu == false && isGameOver == false) { isPaused = !isPaused; PauseScreen.SetActive(isPaused); } if(isPaused) { Time.timeScale = 0f; Debug.Log(Time.timeScale); } else { Time.timeScale = 1f; } } The thing is, only the last modified works, they don´t work at the same time. I have seen that´s because since they do the same thing and Update isn´t affected, it just chooses one and ignore the others, is there a way to circle around this?
@@GameDevBeginner Indeed. It's a bit of a nit-pick, I just think you've done an excellent job at conveying information here, especially geared towards beginners, and that's the step where lots of good and bad habits are formed. Learning the importance of accessibility is a pretty big one. Overall, great videos. Keep rockin brother.
I still have some problems even if I believe this video gave me a lot of useful information. I use timeScale = 0 to pause my game when my level loads because It's supposed to wait for the 3 seconds countdown to end and then timeScale goes back to 1. The problem is that my countdown timer is stopped because of the timeScale = 0 xD I can't figure out how to only make the timer not afected by timeScale =0.
@@GameDevBeginner thanks for the answer, I tried and I cuouldn't make it work so instead, i put my player in a cage for 3 seconds that despawns when the countdown is over xD actually I prefer it that way xd but thank you
Easter Egg Enemy: Unscaled Time Lizard, a lizard with skin instead of scales that is unafected by pausing the game and might just walk up to your character and murder it while the game is paused.
must be a better a better way ... how does it affect UI vfx and tweening? you stop the game, but you will pop up a UI modal dialog, maybe with animated sliding panels....
Don't use time controlled pause! When you need something to need running and another thing to be paused all this work goes to trash. Instead use the last option that the video give. A global variable that objects can read to know if its paused or not.
I just recently added a pause option to my game for the first time and I also did it by setting Time.timeScale = 0 and then having a static bool that kept track of if the game was paused or not. I find doing it this way is pretty easy and intuitive. Also did not know that you can just pause all AudioSources just from one line of code lol, thanks for bringing this up!
Glad it helped!
The same thing could not function properly on the WebGL; both Ignore Time Scale and Remember Read Positions were enabled and the music suddenly went rapidly off while the audio is paused.
The quality of this video is insane, very well scripted and explained, great editing and the information in it is really different from what would you expect from similar videos explaining the same topic, when the video said: "the right way" something in my head told me: this guy knows what he is doing👏
really great content, I feel that we lost brackeys, but we are going to have this guy.
good luck man, really great content.
Thank you so much, I really appreciate it!
Hey, dude, you won't believe the immense help you provided me with! I can't stress enough how grateful I am. Remember when you mentioned that crucial detail at 2:08? Well, I was in the middle of creating the Pause Menu for my game and encountered a perplexing issue. Whenever I transitioned back to the Game Scene from Main Menu, the game froze completely. I tried everything, even deep profiling, but couldn't make any sense of it. All I could observe was that the 'asyncoperationtocomplete()' function was taking an unusually long time. However, thanks to your advice, I decided to set the timescale to 1 before exiting the Game scene, and voila! It miraculously worked! I cannot thank you enough for your invaluable assistance!💕💕
You're welcome! Glad it helped!
Need to pause the game whenever interacting with npc:s to actually be able to use mouse to answer NPC. Helpful tutorial, thank you.
Never quit uploading content, your video quality is really a rare gem to find in the Unity tutorial world these days. This video truly felt like being in a university actually learning something. Keep up the great work.
Thank you!
This is one of the best tutorials I have seen so far. Easy to understand and concise. Thanks a lot!
You're welcome!
I recently had issues in my game with using TimeScale at 0 to pause, as it can potentially stop other scripts in the game midway through execution.
The way I got around this was to have the pause button set a bool, then run the check to set the Timescale to 0 in LateUpdate() as this is more likely to run once everything else is finished in that frame.
I read your blog about pause a game.Amazing blogpost to answer almost every question.Thank you
2:21 very nitpicky catch here, but deltaTime is actually the time between the last frame and the frame before it, not the time between the last and current frame. This is because when deltaTime is called, the program doesn't know when the current frame is going to appear, so deltaTime is a close approximation of how long the current frame will *probably* take to render. Jonas Tyroller did a great video about this, in case you haven't watched it.
Given that this video was released over a year ago, I trust that you've learned a lot more and will have fewer similar errors in your course. I preordered it and am very much looking forward to its release!
No. It's the time between the last frame and the current one (see the Unity docs). In other words, deltaTime is how long the last frame lasted. Of course it would be better if deltaTime was exactly equal to the duration of the current frame, but you would need a time machine for that.
I'm using the new Input System which fires an event when P is pressed. This flips a boolean flag property: it's true, run the Pause logic. If its now become false, run the Unpause logic. Note that this is a C# getter/setter property, not just a boolean variable on its own.
The pause logic sets Time.timeScale to 0 which effectively freezes the main game state, but also uses unscaled time to allow certain other things to carry on (e.g. animations on the pause screen itself). It also uses the Input System to disable/enable certain Actions, to in effect "lock out" certain controls while the game is paused, i.e. the gameplay controls are locked out while the Pause and Quit buttons still work. This prevents the game from going utterly haywire once unpaused again.
I'm convinced my method is absolute crap and fully expect to stumble across a much better and cleaner way at some point.
This is a good quality video that solved my problem. Great job !
My saviour! I didn't know about the unscaled time before and was actually looking for a way to keep UI animations to keep working during a pause.
So glad it helped!
I had just had a similar problem and this is the video that I would have liked to see first.
It has a very good quality, it focuses on explaining the deep instead of the superficial and about the article, the article is INSANE, well explained and looks great.
you gained a spanish speaking subscriber
Thank you!
This was really helpful. I've traditionally just been using a GameState object that sends out a 'GamePaused' event, and having all of the objects that need to respond to that and set their own 'isPaused' state accordingly, but things with Physics of course don't follow that so that's been a challenge. I totally didn't know you could do this, this helps so much, thank you! Love your blog too!
Great to hear it helped!
Here before 1000 subs.
Great video! Editing was simple but good. Explanation was good.
Facts
I’m currently working on a Halo VR game and pausing is turning out to be a pain in the arse, this video helped me so much! Cheers man!
You're welcome!
Really well explained. You deserve a lot more subscribers!
Thank you!
Thanks for this. It helped me solve the problem of letting a user type into an input field and not have the navigation actions take place if you typed an E or other nav key.
Amazing video, love how you explain everything and show different examples. Now to watch all your other vids.
Great tutorial: simple, clear and has good visualization. Thank you👍
You're welcome!
You definitly need more fame and glory for this My god, you made this so damn easy!
Thank you!
Man i don’t event use unity but i watched your entire video! the amount of quality you have is unbelievable, you sure deserve a lot more subs!
Thank you!
to be fair, this would generally apply to most things, as the majority of engines or IMEs that have deltaTime defined will generally also define timescale and unscaledDeltaTime, its pretty standard
I just found your channel, I love your content and your way of explaining things. I just watched the Unity events Video and was impressed so I supped and started watching your other videos. continue with this quality and you'll have 500k sups in no time. good luck!
Thank you!
this is quality stuff. dont stop giving us such amazing content.
This video was absolutely brilliant in how succinct and well-explained it was. The channel deserves way more subscribers.
Out of curiosity - which IDE are you using here? I couldn't work it out.
Thank you! Visual Studio, Gruvbox theme
Thanks! @@GameDevBeginner
First of all, awesome video, thanks a ton for sharing this!
Personally I don't modify the timescale at all, but rather keep a collection of tracked objects that are subscribed to the pause event which pause/unpause their behavior.
Thanks for sharing!
Incredible tutorial, completely surpassed my expectations on this one! Thanks a lot for the help
Thanks so much!
very solid video
Just found your channel, and looking forward to see more.
this a great solution to my the problem i have because like you say time scale effects only the time the have a lot off issues like for example wean you make objects with bool and the are set active or the opiset with having a "is pause" bool or something similar you easy able to make the game better
because you able to disable and enable stern factions like game inputs for example and other stuff you don't wond to happen one other good ideas is to find some way to disabled some off thos faction event the game is not pause for example if someone make i multiplayer game for example it's good idea to make thos system to stop event if not on the pase the game
for example if you play a multiplayer you don't wand to puse the game wean you press the menu but you still wand to disable the player input wean you press this button
Thank you for much. It's really very useful information
Very helpful! Thanks for the video
Love the video, looking forward to more :)
Thank you! more to come.
Very well explained!!
Great video, how can I do when you unplug the console while playing it automatically stops?
Do you mean when the controller's unplugged? The Rewired plugin, as an example, has a disconnect event, that you could subscribe a pause function to. There may well be a native version of that, I would've thought the new Input System has something along those lines too.
Unbelievable video my friend. Thank you :)
Thank you too!
Thank you very much! Great tutorial!
As obvious as it reads(hehe) you can also pause by disabling movement scrips storing vector values, (dont forget about gravity).
Also the way shown in the video is better by any margin of imagination.
Thanks bro, very usefull.
Now I'll use this info for evil.
i stopped game with time.timescale=0; but i cant work this code "Button.interactable=true;" i searched and i probably i need to solve this with time.unscaledTime but how can i do it? help me plz
I enjoyed this video!
Thanks for the great video, didn't you swap isPaused = true and false in the last segment. Or I'm not understanding something.
Thanks! Do you mean @08:18? It checks if the game is already paused and then changes it to the opposite state, which is why it probably looks the wrong way round.
Was hoping for an alternative
Excellent video! I tried using timeScale=0 and it works great in editor, but in a build Windows says the app is unresponsive after being paused for 15-30 seconds, basically crashing the game. I read something about setting it to 0.001f instead, but that didn’t change it. Do you have any idea on where to look for any code that could cause this to happen? Thanks!
I hadn't heard of that but I'm going to look into it. It's possible that something else is happening, so maybe try pausing a build of a blank project to double-check.
way too good ma boy!
I was hoping to find a video on scene management. I have scene1 which on a button click uses a function to load scene2. In the OnSceneLoaded, I check if the scenename is Scene2 and if it is , I tried Gameobject.Find to find a text field in my scene to put something into it. But that line of code is dying, getting a NullReferenceException : Object reference not set to an instance of an object" error. How do I make sure the scene is completely loaded, i.e. all gameobjects on it have been instantiated before I try and Find them? Your help would be most appreciated.😀
Thanks for the tutorial.
I've been having a problem with the pause function in my game: i am doing a game about protecting a cell in the middle of the screen by clicking on targets and dragging others for points, i applied the pause script and it works in stopping time but doesn't prevent interactions with the enemies and the point tokens, they can be dragged and removed, all the tutorial talk about pause time but it doesnt' work when my game doesn't use it or the only thing using time are the enemies slowly going toward the center. Tried 2d and 3d sprites, expecting it was a thing related to it but didn't.
Any help you or anyoen can provide, please?
So my thinking would be that the type of interaction you're talking about is, essentially, input, which because it's not scaled by time in any way, you would need to turn it off yourself. Maybe using a global event in whatever script you're using to trigger and detect those clicks.
@@GameDevBeginner I'll try. I will post how it goes. Thanks.
Been looking into some threads and videos, none managed to answer my question...
You see, i got 3 methods, PauseMenu, Inventory Menu and GameOverScreen, all of then use the same structure as this:
public void PauseMenu()
{
if(Input.GetButtonDown("Pause") && isMenu == false && isGameOver == false)
{
isPaused = !isPaused;
PauseScreen.SetActive(isPaused);
}
if(isPaused)
{
Time.timeScale = 0f;
Debug.Log(Time.timeScale);
}
else
{
Time.timeScale = 1f;
}
}
The thing is, only the last modified works, they don´t work at the same time. I have seen that´s because since they do the same thing and Update isn´t affected, it just chooses one and ignore the others, is there a way to circle around this?
niceeee! 🖤
Good video. Only issue is all those public variables in your scripts... 😞
Thanks! You're right, I should be using Serialize Field if I just want to get it in the Inspector.
@@GameDevBeginner Indeed. It's a bit of a nit-pick, I just think you've done an excellent job at conveying information here, especially geared towards beginners, and that's the step where lots of good and bad habits are formed. Learning the importance of accessibility is a pretty big one. Overall, great videos. Keep rockin brother.
very nice video:)
for some reason my fps goes to -2e+09 when the game pauses why is that?
I still have some problems even if I believe this video gave me a lot of useful information.
I use timeScale = 0 to pause my game when my level loads because It's supposed to wait for the 3 seconds countdown to end and then timeScale goes back to 1.
The problem is that my countdown timer is stopped because of the timeScale = 0 xD
I can't figure out how to only make the timer not afected by timeScale =0.
I tried to put everything in the Update function, it did not do anything
Use Unscaled Delta Time in your countdown timer, that should fix it: docs.unity3d.com/ScriptReference/Time-unscaledDeltaTime.html
@@GameDevBeginner thanks for the answer, I tried and I cuouldn't make it work so instead, i put my player in a cage for 3 seconds that despawns when the countdown is over xD actually I prefer it that way xd but thank you
@@sourisgogo tf ur talking about stupido
Easter Egg Enemy: Unscaled Time Lizard, a lizard with skin instead of scales that is unafected by pausing the game and might just walk up to your character and murder it while the game is paused.
must be a better a better way ... how does it affect UI vfx and tweening? you stop the game, but you will pop up a UI modal dialog, maybe with animated sliding panels....
In most cases, there's an option to use Unscaled Time if you don't want something to be affected by the pause.
pausing games for cats
לא ינום ולא יישן
Don't use time controlled pause! When you need something to need running and another thing to be paused all this work goes to trash. Instead use the last option that the video give. A global variable that objects can read to know if its paused or not.
OK dude
Up
I just don’t scale the time. Like FromSoftware 😂
dosen't work.... but good tutorial