CHANGE SCENE WITH BUTTON IN UNITY 🎮 | Scene Manager in Unity | Learn Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today I'm going to show you how to change scenes inside unity and we're going to do this using a scene manager because I know that you can just kind of like do one line of code and then swap between scenes but I think it's a good idea to do this using a manager so we can actually handle this in a proper way I do also want to mention that this works in 3D and 2D so you don't need to worry about that I'm sitting inside a 2d project it's going to work with 3D as well so if you're sitting with a 3D project this is going to work for you too and the last thing I want to mention is that there's many ways of creating a scene manager and this is going to be my take on how to create a scene manager to get you started with actually handling scenes inside your game everyone watching this video is going to sit with a different type of project so just kind of like take this template that I'm giving you and implement it into your project however you might want to have it best suit your project so with that said let's actually go and talk about how to create a scene manager so as you can see I do have a project open in front of me here this is a 2d urp project but like I said it's not really going to matter if you're sitting in a 3D project or or 2D project doesn't matter it's going to work either way now you might be looking at this thinking oh well Daniel you already have a bunch of things prepared beforehand but it's really not that much and it's something you can very quickly set up in two seconds if you want to have the same thing basically I have two scenes inside my project I have a level one or level zero one and I also have a main menu and these are going to be the two scenes that I want to swap between using my scene manager of course you can't create many more scenes if you want to I just want to just have two scenes that I can swap between with that you can actually see that I have some stuff inside my main menu scene I do actually have a main menu that I created and you'll notice that inside my high key window the way that I like to do things is I like to create a empty game object and I just name it Dash and then something so for example I have one called UI Dash and this is just going to be a top category for my particular UI elements inside my scene so I have everything in sort of a system inside my IQ window so we don't just have a bunch of things in here that are going to make a really long list down so as you can see I have one for my setup which is going to be my main camera or all my cameras and my lighting inside my scene I do have one called managers which is going to be for all my managers inside the game I do have one for my UI element so as you can see right now this is my UI elements inside the scene here and then I have one called environment and this is going to be the one that have all the different game objects inside your scene so like your player your environment like trees and the ground and platforms and stuff like that and inside the UI category here just right clicked and created some sort of UI element which creates a canvas with the UI element inside of it again this is not really like a UI tutorial on how to create a menu I do have one on my channel if you want to check it out basically I just created a canvas with a bunch of UI elements inside of it it's just very basic UI stuff that I did in here I renamed my canvas to main menu instead and just started creating all these elements I have a background a new game button I have an exit Game button and then I have a title the top here and that's all I have for inside the first scene here inside my second scene called level one it's actually even simpler inside level 1 I just created a couple of game elements inside my environment category so I have a ground I have a player that has nothing on top of them I have no scripts or anything they're just like to give us a visuals to see oh this is a new scene I do however inside my UI tab just have a very basic canvas that I renamed to game menu and I just put in a main menu button and again I just sort of like went in and you know changed it so that when I hover my mouse on them you know changes color you could do that very easily if I were to go back inside my main menu here you can see if I were to go down to one of these buttons we can go down and then you can just sort of like change the colors so that when you you know put your mouse curse on top of them they change color so as you can see here they change into a little bit darker version of themselves because I set it up in here but that's basically what I did for my my game elements so what we want to do now is when I click new game it should take me into level one the first thing you need to make sure you do is after you created these two different scenes down here or three or however many scenes you decided to create for this project here is you need to make sure you go inside your build settings so inside file build settings you can see that we now have some scenes inside our scenes in build at the top here you're most likely going to have a sample scene because that's what the unity starts up with when you create a new project just go and right click on it and say remove selection and then drag in the scenes that you have down here inside your scenes folder into your project so they actually get added inside the scenes and builds after adding all your scenes you can actually see that we have some index numbers so over here in the right side and these are actually going to be the the indexes of that particular scene so what you want to make sure you do is that the main menu is at the very top so it has an index as 0 and then level one level two level three however many levels you might have inside your game are gonna go down below you know start with level one then two then three then four and so on so with that you can close this down and now we're actually ready to start scripting so going back inside my assets folder make sure you have a scripts folder so we can have some system inside our project here and inside the scripts folder we're going to right click and create a new c-sharp script now this one is going to be called scenes manager and it's very important that you don't call it scene manager but scenes manager with an S behind it because there is actually a built-in class inside our Unity classes called scene manager and if you rename your script the same way as that particular class then things are going to start getting confused used inside your script so with that go ahead and open it up inside your visual studio and inside Visual Studio we're going to start setting up our manager that is going to manage all the scenes inside the game and this is actually going to be set up in a very typical manager fashion which is in case you haven't learned this before this is going to be very useful for you we're going to go up to the very top here where we have our class scenes manager and we're going to create a static version of this class here which means that we can actually access this class from any script inside our game without having to actually reference the script first so essentially we just directly grab the class because it's static so it's just like top level accessible from anywhere and that can only exist one particular version of this class here inside your game this makes it very good to have as a manager inside your game since there's only one version of it so we can't have like multiple copies anything it's just one top level class that takes care of handling your scenes that you can access from anywhere so the way we create a static scenes manager class is by saying we want to have a public static and then we want to name it the the name of the class so scenes manager with an S behind it and then I want to call it instance again you can rename it to anything you want but instance is just kind of like a good typical name to give all the aesthetic classes here after this we want to delete all the method from inside the class because we don't actually need them and I'm going to create a awake method inside the awake method I just simply want to get instance and set it equal to this which means that right now instance is going to be equal to this class and in this kind of simple way we can now access this class from anywhere just using scenes manager dot instance and then access any kind of method or property or fields that we have inside this particular class here now since we're going to be doing stuff with the scene manager inside Unity we do also need to access the class that I was talking about that is actually called scene manager so at the very top here we're going to say using Unity engine Dot scene management so now we have access to the namespace that have all the different classes that actually has something to do with changing scenes and doing things inside your your game when it comes to scene management and now before we start creating any kind of methods that do something with changing scenes inside your game I actually want to create a enum now an enum is a data type that we have inside c-sharp and I do actually have a video inside my c-sharp course where I actually talk about enums but basically this is just going to have a list of constants inside of it that we cannot change and these constants are going to have an index number so the way we're going to create this enum is going to be by creating a public and then we're going to say enum and I'm going to give it some kind of name so in this case we're just going to call this scene and inside our scene enum I'm just going to go and create a list of items which again are going to be constants so we're just going to go and call this one main menu comma then I'm going to call this one level 0 1 and with that we now have a name for all the scenes we have inside our game so with the enum created here the only thing you need to be aware of is that whenever you add new scenes inside your game you need to do two things you need to add the new scenes inside the enum and they have to be in the same order as the scenes inside your build settings so whenever you create a new scene inside Unity you of course want to drag it into the build settings so it's in there and then you want to go inside the xenome here and make sure that the ordering of the scenes is the same as inside the build settings if you have that everything is going to work perfectly and the reason everything is going to work perfectly that way is because if you were to go inside the enum and take index number zero that is going to be the main menu and index number one is going to be level one and wouldn't you know it if it were to go back inside unity and go inside the build settings these index numbers over here in the right are going to match up with the index numbers inside the enum so that's why I want to make sure you have them in the same order inside the build settings and inside the enum going back inside our scripts what I want to do is I do want to start creating a couple of methods that can actually handle changing scenes inside my game here the first thing I want to create is going to be a load scene method that just simply loads up any kind of scene that I tell it to load up so I'm going to create a public void I'm going to call this one load scene inside the load scene parameters I actually want to include an actual scene that I want to load up which is going to be of the enum scene type and I want to call it something like scene just so we have some kind of name for the placeholder here and of course the reason that we want to have this is so that we can actually take one of these main menu or level one from inside the Eno and pass it inside this method here then it's going to swap scene to that particular scene there so going inside our actual method what I want to do is I want to grab the scene manage your class not the scenes manager class that we created at the top here but the one from inside this particular namespace up here at the top here so we're going to say scene manager dot load scene print disease and then we just simply want to grab the scene and say dot to string and the reason we want to do this is because right now the scene is actually an enum data type but it needs to be passed in as a string so we can actually read it inside our load scene method which of course just takes care of loading up the scene that we tell it to and that's basically all we need to have inside this method here but we do want to have a couple more methods just for the you know just to have them so what I want to do is I want to go down I want to create a new public void and I want to call this one load new game just so we have a particular method that just takes care of loading up a new game so what I want to do is I want to grab our scene manager dot load scene and inside the parentheses here I'm just going to load up the scene enum that we have dot level one dot two string just like we did above and with this we now have a method that only takes care of loading up the first level inside your game so you don't need to call upon load scene and pass in you know level one or anything like that you can just call upon this one and it's going to load up the first level but let's also do another one let's go ahead and create one that loads up the next scene so let's say you finish level one or level two or level three and then you need to load up level four and it just needs to happen automatically I mean just go and do that and that's the reason why we actually needed the index numbers inside the build settings and inside the enum to match up because in order to do this those need to match up so I can create a public void load next scene and in here you want to do the same thing so you want to grab the scene manager dot load scene and then inside our scene manager class we actually have a method called get active scene parentheses dot built index and then we just add one to it so basic what we're just doing here is we're taking the active scene inside Unity so so the one we have right now running inside our game and we want to go inside the build settings and we want to grab the index number which is through the build index and we want to add one to it so if you did things correctly inside the build settings and had level 2 right below level one then this would load up level two and let's go ahead and add a last one in here so let's say public void load main menu parentheses and curly brackets let's just go and make one that loads up the main menu so we can actually go ahead and say instead of new game up here we just want to go in and say we want to grab the main menu instead so now you have a couple of different methods in here that we did together just to kind of like do some different things inside your game again you could just work with load scene and that would be perfectly fine but I think it's just kind of neat to have some methods in here it just takes care of doing one particular thing inside your game like loading up a new game or loading up just the next level inside the build index or loading the main menu or something so you know once you have let's say 10 scenes inside your game then this one particular one up here called load scene is probably going to be very useful to load a particular scene inside your level especially if you have a game where you jump back and forth between levels that one is going to be very useful and really this was just kind of like to show that we had something called active scene and building decks and you know stuff like that you know to share a couple of different ways to you could reference scenes inside your game inside Unity the next thing you want to do is we actually want to make sure that the buttons inside the main menu and inside level 1 is actually working so I can go back and forth between level one and the main menu so what I'm going to do is I'm going to go inside my scripts folder right click create a new script and I'm going to call this one UI main menu now I think that I would probably recommend you start doing once you do a larger project is to create subfolders inside your script folder so you have one called UI and you have one called managers and stuff like that let's actually do that so inside our scripts folder here create a folder call it UI create one called managers and just go ahead and just move them in there so UI main menu is going to go inside our UI and scenes manager is going to go inside managers so what I want to do is I want to go and zap my new script inside the UI folder and I want to open it up inside my visual studio and we're going to go and start creating some Logic for our main menu here which is something I know a lot of people might want to see because they don't know how to create Logic for main menu so the first thing you want to do here is we actually want to load in the UI namespace so we can actually start using UI methods inside our script here so at the very top I'm just going to go ahead and create a Unity engine dot UI so we can actually grab the UI methods and inside my class I'm going to create a field which is going to be one where we actually grab the button that we want to look for and actually do something with so I'm going to do a serialize field and I'm going to call this one button as a type and I'm going to call it start game then I want to go inside unity and actually make sure I pass in the button so we don't accidentally end up not doing that at some point so inside my main menu scene inside my higher key I want to make sure I actually drag the script upon the right thing so I'm going to take my script and I'm going to drag it into my canvas which is the one that I named main menu again if you didn't rename this at any point it's just going to be called canvas inside your example but make sure it's inside the canvas and not inside any of the canvas elements that you have inside of here so if I were to scroll down you can now see that I have my script at the bottom here but I don't have anything passed into the start game button so I actually want to grab my new game so we should actually call a new game not start game and I just want to drag in my new game down here we can actually go back inside our scripts and let's actually just go ahead and rename this to new game just we call it the same thing then let's go and clean things up a little bit here so I'm going to remove my comments and I'm going to remove my updates in inside the start method I'm going to grab my new game and paste it in and then I want to call upon a on click event and then I'm going to use a add listener method which basically means that if I were to click on this button inside my main menu then I want to add a listener that needs to listen for a particular method that I have inside this class here now right now we don't have a method to pass in so let's actually go and create one so I just want to create a private void and I'm going to call this one start game actually no let's call it new game because that actually makes sense to our field up here so new game actually let's call it start new game I'm gonna confuse you now with this we can now go in and actually tell it to load up a scene using one of the methods that we have inside our scenes manager so what I can do is I can reference my scenes manager remember not to write scene manager but scenes manager then we need to call upon the static class itself which is called instance right now if we were to go back inside scenes manager you can see we named it instance up here at the very top which now means that we can actually access any of the methods we have inside that particular class so we can actually say load and then it gives me a couple of options here we can load main menu load new game load next scene or just load a particular scene so in this case here since this is starting a new game I just want to go ahead and load up my new game just to kind of show this since I know some people might have questions about it if I wanted to load a particular scene from inside my load scene method here since we need to pass in something how do we do that if I were to go back here I can actually say load scene and what you would need to do is you actually need to go in and reference to one of the enums from inside the scene manager class so you would actually say scene manager and it actually comes up with a suggestion here so it says C manager dot scene because it knows what type needs to be passed in here and then you just tell it what kind of scene you want to load in so dots and then it gives you suggestions here so you can say level one for example and that's how you would do that just so you know how to use that load scene method from inside our scene manager but let's go and go back here since we want to load a new game which doesn't take any parameters and that's basically all we have to do in here actually you know we need to pass in to start new game method inside our ad listen up here so with that done we now actually need to create a second script for my level one button if we were to actually go back and load up my level one you can see I have a main menu button up here at the top here it's actually going to change the color of that text that looks weird there we go now it has a white text instead so what I want to do is I want to go back inside my scripts folder inside my UI and I want to create a new script that is going to be called UI game menu since that is going to be the menu inside my game here so what I can do is I can actually go ahead and again drag and drop that inside my canvas which is the game menu that I have up here so we're doing the exact same thing as we did with the previous one where we just dragged it into the canvas scroll down to the bottom and then we want to drag in the reference to the button that we actually want to work when we press it right now of course we don't have any code inside the script so let's actually go ahead and load up the UI game menu and inside of here we can actually go and just copy paste a bunch of things from inside the main menu since we need to do the same thing so the unity engine UI we need to have that namespace we do also need to go in and just copy everything from inside the actual class paste it in here make sure you don't copy the class itself because some people do that mistakenly and then everything is going to give you errors then I'm going to rename the field up here so it doesn't say new game but Main menu and then I'm just going to go ahead and copy it down here so we don't get any error messages then I want to call this method down here load main menu and then we want to pass in the right method inside the add listener just so we make sure there's no errors and all you need to do here is now grab the proper method that you want to load from inside our scenes manager so instead of the load new game in this case here we would want to load a main menu and that's basically all we need to do so with that we now just need to do one more thing inside Unity which is to actually create a manager that can have the script on it to actually manage things inside the game so inside the high key inside my managers I'm going to right click it and I'm going to create a empty game object and I'm going to create a scenes manager make sure it is scenes manager then I'm going to drag my manager on top of it so inside my scripts folder I'm going to grab my manager and I'm going to drag it on top of that particular game object do also double check inside your UI that the button is actually set up so if you go inside the canvas and you scroll down that the button is actually the drag and dropped inside the main menu button down here and then I'm going to go back inside my main menu scene make sure you save everything and then I'm going to create a scenes manager inside my managers as well inside the hierarchy so we're going to call this one scenes manager and then I'm just going to drag my script upon it so grab the scenes manage your script and drag it onto your scene's manager and just like that everything is done so if I were to actually go ahead and play this so we can actually test this out you can see that now we have the main menu we can actually hover on top of the buttons it would press it it now loads up my new game which is the first level inside of my game here I can go to the main menu button press it and then we go back to the main menu so as you can see we have a working scenes manager system at least a very basic one inside our game here so we can swap between scenes and load up whatever scene we would tell it to inside our game here so with all that we now learned how to create a basic scenes manager inside our game we might do a loading screen next time so that might be fun so hope you enjoyed and I'll see you guys next time I'd like to take some time at the end here to thank all the different patrons and all the different community members who's been supporting the channel as well as a huge thanks to Kelly corway who's actually sponsoring the channel at the moment so thank you everyone for supporting what I do it means a lot and it's what's keeping this channel going so thank you so much for all the support and I'll see you guys next time thank you
Info
Channel: Dani Krossing
Views: 20,624
Rating: undefined out of 5
Keywords: CHANGE SCENES IN UNITY, unity change scene with button, change scene with button in unity, how to change scenes in unity, scene manager in unity, change scenes using button in unity, unity scene management, unity scene switch, unity scene change, unity scene manager load scene, unity load scene, unity, tutorial, how to load scenes in unity, unity button change scene, unity change scene, unity change scene on button click, how to switch scenes in unity
Id: jrPTpD2eAMw
Channel Id: undefined
Length: 22min 55sec (1375 seconds)
Published: Sat Oct 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.