The right way to pause a game in Unity

  Рет қаралды 56,399

Game Dev Beginner

Game Dev Beginner

Күн бұрын

Want to know how to CODE in Unity? Go here: gamedevbeginner.com/how-to-co...
Learn how to pause a game in Unity, the right way, using time scale.
00:00 Intro
01:13 How to use time scale in Unity
02:15 How time scale works
03:38 What does & doesn't get paused
05:27 Prevent objects from being paused (using Unscaled Time)
06:36 How to pause audio
07:29 How to prevent input when pausing the game
08:37 Outro
You'll learn how to use time scale to pause and unpause your game, what is and isn't affected by a timescale pause and how to manage systems that don't work how you might expect when pausing the game in this way.
Read my full article here: gamedevbeginner.com/the-right...
How to create events in Unity: • Events & Delegates in ...
Outro music: New Year by Bad Snacks
Unity icons created by Freepik - www.flaticon.com/free-icons/u...
Chalkboard image - School rules photo created by kjpargeter: www.freepik.com/photos/school...

Пікірлер: 96
@MyPing0
@MyPing0 Жыл бұрын
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!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Glad it helped!
@michaelalameda2002
@michaelalameda2002 Жыл бұрын
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.
@saitakman5622
@saitakman5622 Жыл бұрын
I read your blog about pause a game.Amazing blogpost to answer almost every question.Thank you
@mattsithknight1530
@mattsithknight1530 Жыл бұрын
Amazing video, love how you explain everything and show different examples. Now to watch all your other vids.
@zaidmermam2524
@zaidmermam2524 Жыл бұрын
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.
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you so much, I really appreciate it!
@Blinkers2007GameDev
@Blinkers2007GameDev 11 ай бұрын
Need to pause the game whenever interacting with npc:s to actually be able to use mouse to answer NPC. Helpful tutorial, thank you.
@alexandrusirbu3083
@alexandrusirbu3083 Жыл бұрын
This is a good quality video that solved my problem. Great job !
@omerlikos2549
@omerlikos2549 Жыл бұрын
this is quality stuff. dont stop giving us such amazing content.
@OrkhanJulfa
@OrkhanJulfa 7 ай бұрын
Thank you very much! Great tutorial!
@random_precision_software
@random_precision_software Жыл бұрын
Just found your channel, and looking forward to see more.
@dykycoend3302
@dykycoend3302 Жыл бұрын
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.
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you!
@calebhorton3689
@calebhorton3689 Жыл бұрын
Very helpful! Thanks for the video
@indieforger2654
@indieforger2654 Жыл бұрын
This is one of the best tutorials I have seen so far. Easy to understand and concise. Thanks a lot!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
You're welcome!
@lln6123
@lln6123 6 ай бұрын
Very well explained!!
@armm6437
@armm6437 Жыл бұрын
Thank you for much. It's really very useful information
@JimSegovia
@JimSegovia Жыл бұрын
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
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you!
@yasseraltamimi6171
@yasseraltamimi6171 Жыл бұрын
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!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you!
@Vleaso
@Vleaso Жыл бұрын
very solid video
@toadmaster
@toadmaster Жыл бұрын
Incredible tutorial, completely surpassed my expectations on this one! Thanks a lot for the help
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thanks so much!
@TopShelfCookieJar
@TopShelfCookieJar Жыл бұрын
Really well explained. You deserve a lot more subscribers!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you!
@jeff_holmes
@jeff_holmes Жыл бұрын
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.
@rahmadhadi464
@rahmadhadi464 Жыл бұрын
Good work bro
@thed100mechanic
@thed100mechanic Жыл бұрын
Love the video, looking forward to more :)
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you! more to come.
@blades575
@blades575 Жыл бұрын
I enjoyed this video!
@rostyslavsmetanin3415
@rostyslavsmetanin3415 Жыл бұрын
Great tutorial: simple, clear and has good visualization. Thank you👍
@GameDevBeginner
@GameDevBeginner Жыл бұрын
You're welcome!
@svenbtb
@svenbtb Жыл бұрын
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!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Great to hear it helped!
@johnthesilverwing2495
@johnthesilverwing2495 Жыл бұрын
Here before 1000 subs. Great video! Editing was simple but good. Explanation was good.
@krazymeanie
@krazymeanie Жыл бұрын
Facts
@LoldemortII
@LoldemortII Жыл бұрын
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.
@GameDevBeginner
@GameDevBeginner Жыл бұрын
So glad it helped!
@NorinDev
@NorinDev 7 ай бұрын
very nice video:)
@ryanem969
@ryanem969 Жыл бұрын
Unbelievable video my friend. Thank you :)
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you too!
@vasileiospgr
@vasileiospgr 10 ай бұрын
niceeee! 🖤
@Ashwin_1198
@Ashwin_1198 Жыл бұрын
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!💕💕
@GameDevBeginner
@GameDevBeginner Жыл бұрын
You're welcome! Glad it helped!
@DashingduoGaming
@DashingduoGaming Жыл бұрын
You definitly need more fame and glory for this My god, you made this so damn easy!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you!
@joshduffety-wong9618
@joshduffety-wong9618 Жыл бұрын
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.
@jeanmestti6195
@jeanmestti6195 Жыл бұрын
way too good ma boy!
@firingpin3520
@firingpin3520 Жыл бұрын
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!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
You're welcome!
@faziarry
@faziarry Жыл бұрын
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!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thank you!
@XiaoYueMao
@XiaoYueMao Жыл бұрын
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
@sceptiletops3760
@sceptiletops3760 5 ай бұрын
Thanks bro, very usefull. Now I'll use this info for evil.
@halfbakedproductions7887
@halfbakedproductions7887 Жыл бұрын
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.
@holybiscuits7714
@holybiscuits7714 13 күн бұрын
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!
@happypixls
@happypixls Жыл бұрын
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.
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thanks for sharing!
@watercat1248
@watercat1248 7 ай бұрын
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
@thescotster19
@thescotster19 6 ай бұрын
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.
@GameDevBeginner
@GameDevBeginner 6 ай бұрын
Thank you! Visual Studio, Gruvbox theme
@thescotster19
@thescotster19 6 ай бұрын
Thanks! @@GameDevBeginner
@macleodgordon
@macleodgordon Жыл бұрын
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.😀
@user-oi3kp7hh1n
@user-oi3kp7hh1n Жыл бұрын
Thanks for the great video, didn't you swap isPaused = true and false in the last segment. Or I'm not understanding something.
@GameDevBeginner
@GameDevBeginner Жыл бұрын
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.
@BlueGooGames
@BlueGooGames Жыл бұрын
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!
@GameDevBeginner
@GameDevBeginner Жыл бұрын
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.
@byram_brt
@byram_brt 8 ай бұрын
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
@DevDunkStudio
@DevDunkStudio Жыл бұрын
Was hoping for an alternative
@faisalalhaddad5537
@faisalalhaddad5537 Жыл бұрын
Great video, how can I do when you unplug the console while playing it automatically stops?
@GameDevBeginner
@GameDevBeginner Жыл бұрын
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.
@gekkek2005
@gekkek2005 Жыл бұрын
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.
@crazyone3494
@crazyone3494 8 ай бұрын
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?
@user-tq7bd7jb3m
@user-tq7bd7jb3m Жыл бұрын
for some reason my fps goes to -2e+09 when the game pauses why is that?
@doarner
@doarner Жыл бұрын
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?
@GameDevBeginner
@GameDevBeginner Жыл бұрын
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.
@doarner
@doarner Жыл бұрын
@@GameDevBeginner I'll try. I will post how it goes. Thanks.
@ourinterface7077
@ourinterface7077 10 ай бұрын
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.
@Vastlee
@Vastlee Жыл бұрын
Good video. Only issue is all those public variables in your scripts... 😞
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Thanks! You're right, I should be using Serialize Field if I just want to get it in the Inspector.
@Vastlee
@Vastlee Жыл бұрын
@@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.
@sourisgogo
@sourisgogo Жыл бұрын
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.
@sourisgogo
@sourisgogo Жыл бұрын
I tried to put everything in the Update function, it did not do anything
@GameDevBeginner
@GameDevBeginner Жыл бұрын
Use Unscaled Delta Time in your countdown timer, that should fix it: docs.unity3d.com/ScriptReference/Time-unscaledDeltaTime.html
@sourisgogo
@sourisgogo Жыл бұрын
@@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
@dr.angerous
@dr.angerous Жыл бұрын
@@sourisgogo tf ur talking about stupido
@halivudestevez2
@halivudestevez2 22 күн бұрын
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....
@GameDevBeginner
@GameDevBeginner 22 күн бұрын
In most cases, there's an option to use Unscaled Time if you don't want something to be affected by the pause.
@MarekNijaki
@MarekNijaki Жыл бұрын
Up
@Ella_Kitcat.71GFbleny
@Ella_Kitcat.71GFbleny 11 ай бұрын
OK dude
@EnderElohim
@EnderElohim Жыл бұрын
pausing games for cats לא ינום ולא יישן
@tomasramirez269
@tomasramirez269 20 күн бұрын
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.
@nikz000
@nikz000 17 күн бұрын
I just don’t scale the time. Like FromSoftware 😂
Coroutines in Unity (how & when to use them)
12:35
Game Dev Beginner
Рет қаралды 23 М.
PAUSE MENU in Unity
12:13
Brackeys
Рет қаралды 980 М.
Самое Романтичное Видео ❤️
00:16
Глеб Рандалайнен
Рет қаралды 6 МЛН
Events & Delegates in Unity
13:20
Game Dev Beginner
Рет қаралды 50 М.
Raycasts in Unity (made easy)
16:30
Game Dev Beginner
Рет қаралды 84 М.
So you want to make a Game Engine!? (WATCH THIS before you start)
14:39
Giant Sloth Games
Рет қаралды 274 М.
How do non-euclidean games work? | Bitwise
14:19
DigiDigger
Рет қаралды 2,4 МЛН
A new way to generate worlds (stitched WFC)
10:51
Watt Designs
Рет қаралды 515 М.
10 Things You NEED to Be Doing in Unity
11:40
Tarodev
Рет қаралды 127 М.
The Power of Scriptable Objects as Middle-Men
17:41
samyam
Рет қаралды 119 М.
I Wish I Had Known This Before I Started Unity Game Development...
11:11
The right way to Lerp in Unity
9:38
Game Dev Beginner
Рет қаралды 17 М.
The Art of Game Optimization
10:18
Worlds In Motion
Рет қаралды 257 М.
Самое Романтичное Видео ❤️
00:16
Глеб Рандалайнен
Рет қаралды 6 МЛН