Pause in Unity WITHOUT Timescale

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
most people pause our unit again by setting the time scale to zero but it's actually a much better way to do this using events here's how you do it in your project create a new script called game state this will be an enum with two values gameplay and paused go back to your project and create another script called game state manager this will be a singleton since we only want one across the game add a private constructor a private static instance field a static instance property with the getter this will return or create a new instance create a delegate called gamestatechangehandler that takes the gamestay as a perimeter then create a public event using this delegate called on game state changed we'll find this event when we want to change the state speaking of state create a public game state property called current game state with a private setter now create a public method called set state that takes a game status parameter first we'll check the status actually change before we continue next set the current game state to the new state and then call invoke on on-game state changed to fire the event now we need to listen for the event find a script will be affected by pausing i'll use this player movement script as an example add a message called on game state change that takes a game state as a parameter set the components enable property based on the gamestay parameter in the components awake method subscribe to the gamestickmanager's ongamestate changed event in the components ondestroy method unsubscribe from the event this will prevent memory leaks and errors after the object is destroyed now let's set the game state back in your project create a new script called pause controller in the update method check if the escape key is being pressed if it is we'll get the current state from the game state manager and toggle it finally we call set state on the game state manager to notify all our listeners back in your project add a game object called game manager and add this pause game control script to it when you enter play mode and hit escape the game will pause and resume but why use this instead of time scale first setting time scale can break your ui animations and any timers are running in the background second this approach can be extended to handle other states like level loading or cutscene plane finally it offers more control over how each component is affected by the game being paused for example it can be used to show or hide a pause menu play or stop an audio source or enable or disable input controls using the unity input system you can find all the code links in the description if you're enjoying the video why not leave a like and subscribe for more see you later
Info
Channel: James Makes Games
Views: 118,344
Rating: undefined out of 5
Keywords: pause without timescale unity, pause with events unity, pause unity, pause timescale unity, pause time scale unity, pause without timescale, unity pause game without time scale, pause unity game, unity pause scene, how to pause unity, how to pause in unity, pause game unity, unity pause game, unity pause tutorial, pause unity in 2 mins, active pause unity, james makes games
Id: KPaEnLpu57s
Channel Id: undefined
Length: 2min 0sec (120 seconds)
Published: Fri May 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.