Unity Use a Button to Change Scenes 2023 (Updated) | Unity Tutorial for Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey there and welcome to my Channel today I'm going to be showing you two ways to load a scene via a button in unity let's get started I always like to show things in context just so you know how this is really going to work so here's the level map of a game that I've been working on for the past several months so now I'm going to create a main menu to pop up before my level map now that I have my new scene I'm just going to right click go to UI and then add UI canvas adding the canvas is going to be pretty important in this step and then next I'm just going to kind of speed fill in some items just so you have some context of how this is going to work but not diving too deep into how to make the UI so I'm just going to pre-fill some assets here that I've already made feel free to make it your own and now that we have our context let's get into actually programming the button now I need to create a scene loader script and this is going to go on our canvas game object not on our button so let's go over to our canvas select add component and then new script now opening up visual studio since we do want to load scenes with this we're going to need to use a Unity engine dot scene management and now we're going to remove the start and update methods because we want to create functions that are called on click so we will not need these now when you're trying to load a scene from an on click event there's two ways to do this now the first way that I'm showing you is to load the scene by its direct name I personally usually prefer this method just feels a little bit easier to remember but just keep in mind if you do change your C names in the future that you're going to have to change this script as well we can accomplish this by accessing scenemanager.load scene and then the name of your scene as it appears in your build settings now the other way we can load a scene with a button is to access that scenes build index so I'm going to create a function load next in build now in this method I want to tell Unity to load a scene but based on the active scenes build index I'm basically just asking Unity okay what scene am I in and then load the scene that's next in the build index heading back to Unity let's take a look at the build index currently I only have two scenes in my build index so I need to make sure to add the scene that I've been working on and place it with a build index of zero that way when you build your app that's the scene that loads first now something important to note here that if you do organize your scenes you'll see that my game scene is a couple of folders deep you'll need to make sure that the name in your build index is exactly the name that you are referencing in your code now that we have this properly set up let's now start attaching our logic to our buttons I'm just shifting around the panels here so it's a little bit easier to see going back to our button right now this is just an image so I'm going to need to add the button component on here adding that gives us access to the on click event listener which is what we're going to be using to make our logic work since the script that we wrote is on our canvas game object we're going to drag the canvas into this section right here and then we're going to select one of the functions that we just wrote for this button I'm using the load scene by name function and to prove to you that both of these methods work I'm going to duplicate this button but instead of using the load scene by name function I'm going to use the load next in build function let's make sure everything in our game is saved and let's test it out first button looks like it's working and second button looks like it's working and this concludes today's episode of how to load a scene in unity using a button if you like this content please like And subscribe and I will see you in the next one thanks for watching [Music]
Info
Channel: Rigor Mortis Tortoise
Views: 1,635
Rating: undefined out of 5
Keywords: unity tutorial, change scenes in unity, how to change scenes in unity 2d, unity how to change scenes, how to change scenes in unity, how to change scenes with a button in unity, unity c# tutorial, unity ui button tutorial, switch between scenes, how to load scenes in unity, unity change scene, game development, unity change scene with button, unity button change scene, unity change scene on button click, how to switch scenes in unity, how to change scene in unity
Id: GTPrZfwb56E
Channel Id: undefined
Length: 3min 59sec (239 seconds)
Published: Thu Jan 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.