Change Scenes in Unity with a Button (basic tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is a quick video on showing how you can use a button in unity to switch scenes I'll be showing this with both the text mesh Pro and Legacy buttons let's get started okay for my example I'm just using a basic 2D project but this could also be programmed the same way using 3D I need to do a little bit of setup to start so let's get our two scenes made so for this first scene I will want a button go to UI and for this first one let's use the Legacy one and I will go to button now it pops in kind of at a weird spot and if you want to see it more clearly you can simply double click on that button and it centers it if you'd like to see where it is in relation to the canvas you can double click on the canvas and you should see an outline there and we can move the button to where we'd like it to go so I'm clicking on the Move tool going to the button and moving that so this is the Legacy button and I'm just real mousing in to look a little bit closer so if I'd like to make a text mesh Pro button I'll show you that as well the coding ends up being the same so if I wanted a text mesh Pro button I would say plus UI and go here to button text mesh Pro that one will give us more control over the look if you're using a text mesh Pro text or button it will ask you to import some TMP essential so if that pops up go ahead and click okay it'll import some things to your project and then you can close this window and in this case here is my button for both of these if I go to the Little Triangle over in the hierarchy underneath button there is a text element and if I click on that and go over to the inspector that's where I can change the text so I could say go to scene two and I could do the same for the text mesh Pro so here I'm going to do the little arrow click on the text element and over in the inspector now for yours you'd probably just make one button I just wanted to show you how they work about the same if I hit play we can see where it is in the scene and I just have my buttons there okay so that's all set up let me save my scene and I'm going to go ahead and say save as and let's name it something so I'll go in scenes and let's call this I'll call this um scene one okay so now let's make the scene that we would like to go to and then we'll come back and do the code after so to make a new scene I go up to file new scene and since I'm doing 2D I'm just going to do the basic 2D and notice it's just a blank scene so now I'm just going to put some text just so we know that we're on the next scene so to add this text I'm going to click the plus UI and I'll choose the text message Pro because it can look a little nicer over in the inspector I can change the text here so I'm going to say this is scene to I can use this bounding box or rectangle tool to resize the text box in the inspector I can change the alignment and color and effects and things like that however I'd like it so now I want to go ahead and save this scene two so I'll go file save as and along with my scenes I'm just going to call it scene two okay so let's go back to the scene with the buttons so I can go to file open recent scenes and go back to scene one now I need to make some code to control that these buttons will take me to the next scene so in the assets I'm going to rightclick create C script and I can name this whatever I want I'm just going to call this change scenes okay so let's go into that c script so I'm just double clicking inside of here since I am only using this script to change scenes I can actually get rid of these start and update methods I'll be making a new method to control my button so I'm going to go ahead and make a method I'm going to call it public I need to say void because it's not returning a value and I'll call this go to scene two need parentheses and curly braces now this is the method that I will connect to my button and here is where I'll say go to the other scene to do that we're going to be using something called called scene manager now it will need a directive up here but it often puts that in for you automatically so I'm going to go ahead and see if it does that and if not I will type it in after so to go to the next scene we will type scene manager load scene and then in the parenthesis I'm going to say the scene name in quotes a semicolon now notice it automatically added this directive at the top so if that doesn't come in you will need to type using Unity engine. scen management if that's not there I'm just going to comment that out for a second it won't know how to run this and if there's like a little red line under scene manager it's probably because that directive isn't in place so this is really all the code I need so I'm going to go ahead and save this and back to Unity and then what I need to do is connect these buttons to the script so in the buttons themselves if you click on them and go to the inspector and scroll down there is an onclick section and that's where we be we'll be connecting the code and this works the same for both of them so on onclick I click the plus to add a an action it will run what we need to do is connect our script to Something in the hierarchy and then put that object in this box so there are different places you could put it you could make a new game object for the script I've seen people connect it to the canvas since it's controlling the UI I've seen people put it on the button I'm going to put mine on my canvas so now the canvas has the change scene script attached to it so back to the button I want to make sure that the canvas is attached to here because that holds my script so I can do that a couple of ways I can while this is up I can click and drag my canvas to this or I can click on this little dot scene and click my canvas there whichever works better for you then we need to decide what method or function we're running so notice it still says no function so I'm going to click on that go down to my script name which was change scenes and then choose my method which in this case was called go to scene two so now if I click on the button it will go to the script that's attach to the canvas and run my goto scene 2 method so I'm going to save this and I'm skipping one step right now just to show you a common error and then I'll show you how to fix it so if I hit play and I click on go to scene two it's not working yet right and also down below it has a little error and in it it says scene two couldn't be loaded because it has not been added to the build settings or the asset bundle has not been loaded so that is a clue that we forgot to add the scene we're trying to load into the build settings to add the scene to to the build settings go under file build settings and notice up top it shows what scenes are in the build so right now the sample scene which was the very first one which we're not actually even using is there and we want to add the one with the buttons and we also want the second scene so I can click add open scenes but notice it's only adding scene one right now because that's the one that's open so I could open the other one and click on ADD open scenes another way I can add my additional scenes is go to my my project in my scenes and notice I want scene two and I can grab it and drag it into my scenes so now it has access to these scenes I also want to point out this part where it says scene two that is the name if we want to actually call the scene by name we would call it scene two notice no spaces there's also a number on the right where it shows the index so I can also refer to that scene by its index number so when I open it I could say load scene two instead of the word scene two so I'm going to close this let's hit play and when I run it it takes us to scene two same thing would go for this text mesh Pro one the process works exactly the same for the buttons um we've got the onclick I can add the script so I want this to be the canvas object so I drag that over I choose my class so that's change scenes and I'm going to go to scene two and if I hit play that will also work so either one works the same one more thing back to the script here notice what I was talking about the index we've got Scene manager load scene I can say scene two but I could also just use the index number there if I'd rather so that would work too so I'm going to save this and let's run it just to verify so it works the same so I hope this helps you if you'd like additional scenes you can use that in any part of your code to go to another scene it's pretty easy if you find this helpful please check out my channel for other unity and programming tips
Info
Channel: Digestible
Views: 637
Rating: undefined out of 5
Keywords: Change Scenes Unity, SceneManager.LoadScene, SceneManager Unity, SceneManagement Unity, Unity C# tutorial, Unity Tutorial
Id: 3SdMFPdSi7M
Channel Id: undefined
Length: 10min 57sec (657 seconds)
Published: Mon Oct 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.