The right way to pause a game in Unity

Поделиться
HTML-код
  • Опубликовано: 21 дек 2024

Комментарии • 97

  • @MyPing0
    @MyPing0 2 года назад +73

    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  2 года назад +4

      Glad it helped!

    • @michaelalameda2002
      @michaelalameda2002 Год назад +1

      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.

  • @zaidmermam2524
    @zaidmermam2524 2 года назад +36

    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  2 года назад +1

      Thank you so much, I really appreciate it!

  • @Ashwin_1198
    @Ashwin_1198 Год назад +3

    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!💕💕

  • @Blinkers2007GameDev
    @Blinkers2007GameDev Год назад +1

    Need to pause the game whenever interacting with npc:s to actually be able to use mouse to answer NPC. Helpful tutorial, thank you.

  • @dykycoend3302
    @dykycoend3302 Год назад +2

    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.

  • @indieforger2654
    @indieforger2654 2 года назад +7

    This is one of the best tutorials I have seen so far. Easy to understand and concise. Thanks a lot!

  • @joshduffety-wong9618
    @joshduffety-wong9618 Год назад +3

    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.

  • @saitakman5622
    @saitakman5622 Год назад

    I read your blog about pause a game.Amazing blogpost to answer almost every question.Thank you

  • @holybiscuits7714
    @holybiscuits7714 5 месяцев назад +1

    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!

    • @cfffba
      @cfffba 3 месяца назад

      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.

  • @halfbakedproductions7887
    @halfbakedproductions7887 Год назад +1

    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.

  • @alexandrusirbu3083
    @alexandrusirbu3083 Год назад +1

    This is a good quality video that solved my problem. Great job !

  • @LoldemortII
    @LoldemortII 2 года назад

    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.

  • @JimSegovia
    @JimSegovia 2 года назад

    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

  • @svenbtb
    @svenbtb Год назад +1

    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!

  • @johnthesilverwing2495
    @johnthesilverwing2495 2 года назад +1

    Here before 1000 subs.
    Great video! Editing was simple but good. Explanation was good.

  • @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!

  • @TopShelfCookieJar
    @TopShelfCookieJar 2 года назад +2

    Really well explained. You deserve a lot more subscribers!

  • @jeff_holmes
    @jeff_holmes 2 года назад

    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.

  • @mattsithknight
    @mattsithknight Год назад +1

    Amazing video, love how you explain everything and show different examples. Now to watch all your other vids.

  • @rostyslavsmetanin3415
    @rostyslavsmetanin3415 2 года назад +1

    Great tutorial: simple, clear and has good visualization. Thank you👍

  • @VoxelsmithGaming
    @VoxelsmithGaming 2 года назад

    You definitly need more fame and glory for this My god, you made this so damn easy!

  • @faziarry
    @faziarry 2 года назад

    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  2 года назад +2

      Thank you!

    • @XiaoYueMao
      @XiaoYueMao 2 года назад +1

      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

  • @yasseraltamimi6171
    @yasseraltamimi6171 2 года назад

    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!

  • @omerlikos2549
    @omerlikos2549 Год назад

    this is quality stuff. dont stop giving us such amazing content.

  • @thescotster19
    @thescotster19 Год назад

    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.

  • @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.

  • @toadmaster
    @toadmaster 2 года назад

    Incredible tutorial, completely surpassed my expectations on this one! Thanks a lot for the help

  • @Vleaso
    @Vleaso 2 года назад +2

    very solid video

  • @random_precision_software
    @random_precision_software 2 года назад

    Just found your channel, and looking forward to see more.

  • @watercat1248
    @watercat1248 Год назад

    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

  • @armm6437
    @armm6437 2 года назад

    Thank you for much. It's really very useful information

  • @calebhorton3689
    @calebhorton3689 Год назад

    Very helpful! Thanks for the video

  • @thed100mechanic
    @thed100mechanic 2 года назад

    Love the video, looking forward to more :)

  • @lln6123
    @lln6123 11 месяцев назад

    Very well explained!!

  • @faisalalhaddad5537
    @faisalalhaddad5537 2 года назад +1

    Great video, how can I do when you unplug the console while playing it automatically stops?

    • @GameDevBeginner
      @GameDevBeginner  2 года назад

      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.

  • @ryanem969
    @ryanem969 2 года назад

    Unbelievable video my friend. Thank you :)

  • @OrkhanJulfa
    @OrkhanJulfa Год назад

    Thank you very much! Great tutorial!

  • @gekkek2005
    @gekkek2005 2 года назад

    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.

  • @sceptiletops3760
    @sceptiletops3760 10 месяцев назад

    Thanks bro, very usefull.
    Now I'll use this info for evil.

  • @byram_brt
    @byram_brt Год назад +1

    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

  • @blades575
    @blades575 2 года назад

    I enjoyed this video!

  • @skrewdeiver
    @skrewdeiver 2 года назад

    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  2 года назад

      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.

  • @DevDunkStudio
    @DevDunkStudio 2 года назад

    Was hoping for an alternative

  • @BlueGooGames
    @BlueGooGames 2 года назад

    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  2 года назад

      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.

  • @jeanmestti6195
    @jeanmestti6195 2 года назад

    way too good ma boy!

  • @macleodgordon
    @macleodgordon 2 года назад

    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.😀

  • @doarner
    @doarner 2 года назад

    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  2 года назад +1

      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 2 года назад

      @@GameDevBeginner I'll try. I will post how it goes. Thanks.

  • @crazyone3494
    @crazyone3494 Год назад

    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?

  • @vasileiospgr
    @vasileiospgr Год назад

    niceeee! 🖤

  • @Vastlee
    @Vastlee 2 года назад

    Good video. Only issue is all those public variables in your scripts... 😞

    • @GameDevBeginner
      @GameDevBeginner  2 года назад

      Thanks! You're right, I should be using Serialize Field if I just want to get it in the Inspector.

    • @Vastlee
      @Vastlee 2 года назад

      @@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.

  • @NorinDev
    @NorinDev Год назад

    very nice video:)

  • @user-tq7bd7jb3m
    @user-tq7bd7jb3m Год назад

    for some reason my fps goes to -2e+09 when the game pauses why is that?

  • @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  Год назад +1

      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

  • @ourinterface7077
    @ourinterface7077 Год назад

    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.

  • @halivudestevez2
    @halivudestevez2 6 месяцев назад

    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  6 месяцев назад

      In most cases, there's an option to use Unscaled Time if you don't want something to be affected by the pause.

  • @EnderElohim
    @EnderElohim 2 года назад +1

    pausing games for cats
    לא ינום ולא יישן

  • @tramidev
    @tramidev 6 месяцев назад

    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.

  • @ellie_kitten_xd_cutie_pie
    @ellie_kitten_xd_cutie_pie Год назад

    OK dude

  • @MarekNijaki
    @MarekNijaki 2 года назад

    Up

  • @nikz000
    @nikz000 6 месяцев назад

    I just don’t scale the time. Like FromSoftware 😂

  • @Catalin-i1o
    @Catalin-i1o 12 дней назад

    dosen't work.... but good tutorial