Unity - How to Use a Button To Switch Between Scenes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi so today I'll be showing you how to create a button that switches scenes in unity so to start off you probably just want to go to your assets scenes folder and then rename some rename your scene to something well while you had it you should also create another scene which will be switching to and now it's called secondary and stay on your main scene and then go over to your hierarchy main right click go on UI then button and just rename a button to something else I guess I'll call it play and then I'll just Center my button in the middle while I'm at it I'll go over to double click on that secondary scene save just the case and right-click do the same thing create another button center that again just for this video sake and I call this back now over here in case you don't already have a scenes folder I'd right-click on assets create folder I mean I scripts folder my line that's because scripts folder and then right-click on the scripts folder go to create c-sharp script and this call it whatever you'd like I'll call this means scene switcher I'll just double click on it and then it will open up visual studio over here so we won't be needing any of this let's go over to using to the toppika thing and type in using unity engine dot scene management alright and then over here let's make a function called play game public void play game and then in this we will type in scene manager dot load scene then bracket scene manager dot get active scene bracket dot building X and then we'd like to add one and then end it off with this you know with this just go back over to unity and drag this scene switchers square over to the let's go to the main first naturally go over here go to the main scene make sure on that and then drag the scenes which were script onto the canvas for the main scene that should be going on and after that click on the button go over to on click click the little plus button drag canvas right there click on this drop down menu and go to scene switcher and you should see there's no play game yet right this is the thing that would happen if you don't save so save go over here see switcher and play games right there cool now this will just take us to the next scene but the back button won't do anything yet so let's go over here to the other scene that we'll be switching to do the same thing drag the scene switcher onto this onto here go to the button and just drag the canvas right there now before we could do that we have to meet the function that makes it go back you can't use the same one so let's try a public void back and just take this code right here do two moments more but instead of that just subtract save remember to save or I won't show up and again over to drop down menu scene switcher back now just save once more and just go to main again then go on file build settings and then just make sure you have this right but if you don't for some reason just go to main first and then click Add open scene and then go to secondary and click Add open scene once more and it's 0 to 1 right so what the code pretty much does is it adds it goes from here and then adds 1 goes to number 1 but then when we go to secondary we said subtract 1 write build index this is a building next here there's no chart on takes us back so now that we got all this done we should be able to run this and it should work back play back play works perfectly fine thank you for watching and I hope you have a nice day bye
Info
Channel: Heer
Views: 94,325
Rating: undefined out of 5
Keywords: unity, unity game engine, unity tutorial, tutorial, coding, unity game, unity how to switch scenes, unity how to use button, unity how to use button to switch scenes, how to switch scenes in unity, how to use a button in unity, unity how to switch scenes with a button, using button to switch scenes, unity button tutorial, unity scene switching tutorial, unity how to switch scenes tutorial
Id: PpIkrff7bKU
Channel Id: undefined
Length: 4min 20sec (260 seconds)
Published: Sun Jan 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.