PAUSE MENU in Unity! 2020 Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm gonna show you how to make a pause menu in unity so whenever you're playing the game and if you hit the pause button it pauses the game and if you hit resume it resumes the game and I will also show you how to hook up all of these buttons over here so with that being said let's jump right in alright so I'm going to create a UI canvas I already have one but I'm just gonna make another one now under this canvas you can create a UI panel right there I'm gonna come in here and make the color black so that whenever we pause it's gonna darken the background and add focus to our buttons so now I'm just gonna rename this to pause menu and then onto this pause menu I'll go under UI and select button now I'll take this button and scale it up pretty big just like okay I'm not gonna go there so just put a 10 in the middle okay now go under your button and just delete this text why delete this text because this is the old text this is I like to call the legacy text nobody needs this it's very bad quality so we'll just go ahead and create a text message is just a lot better has more settings and even has gradients just just add and I'm going to rename this to just text because it's child it to the button don't need to rename every individual child and I'll rename this to resume button because I think that's what it does and now I'm gonna add a source image for my button so I'm just gonna go and find my board here I'm gonna drag that into the source image and you know what let's make that a bit bigger okay yeah be professional okay so next we need to go into the rect transform right here and holding down shift and alt you can stretch it to the parent so that it kind of centers the text to the button and then just align it on both axes so it's dead in the middle and then I think I'm gonna bump up the font size to something like 120 so it's just really big and have a font imported this is kind of a Japanese looking one and I'll just say resume if I could spell it right that is a bit too big maybe 110 there you go so we got the fundamentals of our button now we can just copy and paste this button so I'll just roll this button up and duplicate it twice one for the menu button and for the quit button now I'll just select both of these and drag them down same for this button the quit button and then I'll rename this to menu button and this one to quit button if you're a quitter press this button alright so go into this text and say menu and under the quit buttons text say quitter no no no we're professional it says quit all right so look how easy that was guys it was only two minutes and you got all this beautiful yard right in front of you now to add function to this I'm going to go on to my canvas so the parent of all of this I'm going to add a component and I already have it spelled out but just pause I already have a pause menu but I can't make a second one so I'll just call it pause new script create an ADD and go ahead and open it up in visual studio alright so let me zoom in for you guys alright so we don't need the start function we are not going to use that for anything but we do need a bull to check if we are currently paused so I'm gonna make that going to be a public static and why static this is kind of hard to explain let's see okay so static basically what static is if I go to my wild fish and just go into a wake and say if pause which is my script and then I got to make this bull first actually bool is game paused to say if it's paused and I'll initialize that as false so we're not paused at the start now if I go back to this script and I can just say pause dot is game paused and you can see it's right there you can access it right away which is awesome we don't need to find object of type or anything but if I did not make the static you go in here and you get a big fat error so that's why it's going to be a static and also static variables don't need an instance of a class so that's also pretty cool so inside of our update function what we're gonna need to do is we're gonna say if input dot git key down so the moment we press down this key and we're gonna say what key so key code dot escape which is the regular pause button from almost every game right so if we press this big red button this stuff is gonna happen in here first I want to check if the game is paused so if is game paused so if it is paused then we want to resume the game and I'm gonna make these functions in a second else meaning if the game is not paused then we want to pause it now so pause the game and there you go and now we actually need to make these functions so let's go ahead and do that so void resumed game and now most of you might be thinking like why don't I just put private void in front like that would be more cleaner but I've been coding a bunch and I just don't like the extra code to look at and void is still private so it doesn't even matter it's kind of my own preference for coding though but anyways let's just create the pause function now so void pause game all right so when we pause the game we need to do three things we need to turn on the pause menu we need to stop time completely and then after that we need to say the game is paused is equal to true because when we pause well it's paused so to set active the pause menu we need a reference to it so I'm gonna go up here and say serialize field and if you're wondering what a serialized field is it's the same way as saying public so that you can see it inside of your inspector but this is a little bit more private so that other classes don't need to access it so if you are making a variable that no other class really needs then this is just a cleaner way to do it so serialize the field and this is going to be of type game object and this is my pause menu and right away because I always seem to forget this just tap on your canvas and reference this exact pause menu in this field here and now we're all set and we don't have to forget it later so now that we have a reference to our pause menu we can go ahead and set it active so pause menu dot set active and then we could give it a value here so we're gonna set it active true and then we want to stop time so time dot timescale is equal to zero so nothing will move at all and then right after all of that we need to say finally the game is paused it is equal to true so now for our resume game function we'll just copy all this code but do everything the opposite turn off the pause menu reset the time scale back to 1 so completely normal and the game is no longer paused all right so with this pretty simple code we can go ahead and check it out and just so you guys don't get stuck make sure you did not put the script on to your pause menu because you can't run code off of a disabled object so if you say pause on a disabled object it's not gonna pause so that's why I put it on my canvas here but I'm going to disable my pause menu so that we do not see that at the moment so when I'm playing my game and I hit pause boom there it is and then I hit escape again and it unpause --is that's already a lot of good functionality but now we need to make all these buttons work jump back in and put on nerd glasses back on I want my button to access this resume game function so it's gonna need to be public if it's not public your button will not be able to access it so now I'm going to make a public void load menu so you could load the main menu and then another public void again these has to be public quit game and now in the load menu function all we will do is reload our current scene why because I don't have a main menu but I want to show you guys what happens when we tend to load scenes in a pause menu so to blow the scene we'll just go up here and say using Unity engine dot scene management and now it allows us to access the scene manager so scene manager dot load scene and we can put in a number of things here we could say load the third scene in the build index and keep in mind that it starts off at zero and we could also give it a name of the scene so we can say load menu but right now as I said I'm reloading my current scenes so I'm gonna say scene manager this is how you get your current scene dot get active scene some parentheses dot build index so if we wanted to load the next scene in the build index we could simply say plus one but I'm gonna reload the same scene cool that is done let's go to our quick game function and all we have to do is say application dot quitter oh wait yeah quit we don't frown upon quitters I forgot so now that we have that lets go ahead and debug log quit because you can't actually quit inside of unity I know there's a way that you can do it but I'm gonna make sure that we log it in the console just so we know that everything is functioning so now make sure you save your script and jump back into unity and I'll activate my pause menu and I'll go to my resume button and scroll all the way down and you'll see this on click event here you'll want to add an event and we want to reference our canvas because it has the paused script on it you can't reference an object that doesn't have our pause script on it because as you can see here there it is so now let's go back to our reason button and select function and our script is called pause and what we want to do is resume game again you won't see them here if they're not public if you didn't save your script if you're referencing the wrong object if your functions are static I'm just making a checklist for you so resume the game go to menu button same exact thing ref the canvas function pause this time we load the menu quit button at an event reference the canvas go to function pause and quit game disable the pause menu and if we play the game we can go around and then pause and then we can also resume the game with a button and then when we pause again we could also quit and it logs in the console right here quit and if we press menu again it's gonna reload our current scene because I don't have a menu but if we hit menu we get this problem everything is completely frozen why is that because as soon as we pause our game our time scale is set to zero and until we resume our game it sets back to normal but when we load a different scene we don't necessarily change the time back to normal so we just need to copy this right here whenever you blow this scene and I'll save that up jump back into unity and whenever we hit play pause and then hit the menu it's all working like a charm so basically all I really did was I made a bool to check if the game is paused and every single frame in our update function we check if we hit that big red button and whenever we do hit that big red button we pause the game and what pause game does is it reaches in our pocket and gets that pause menu for us it stops everything and it sets this bull right here to true so that's actually it for this video I hope you guys enjoyed if this video helped you out in any way please consider subscribing to the channel looking forward to more videos and with that being said I'll see you next time
Info
Channel: Kap Koder
Views: 12,546
Rating: undefined out of 5
Keywords: Pause, menu, unity, tutorial, how, to, make, c#, c sharp, kap, koder, resume, game, quit
Id: eC05j7rh_LM
Channel Id: undefined
Length: 12min 51sec (771 seconds)
Published: Tue Jun 09 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.