Switching Between Scenes in Unity 3D using UI Button

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to rags guys so in today's session we just see how to switch scenes using unity button UI button so in order to do that we need to have a couple of scenes at least so let me go ahead and first create the simple scene so in order to do that just create a simple UI and the panel of course and let me go to the game mode and let me just change this one to full white okay so let me just name this ass it's just simply name there's a scene 1 scene 1 so let me align it to the center and increase the font size here okay so just change it to 1080 yeah that's looks better now this is going to be my scene 1 and here we need a button in order to switch to scene the next scene which is our scene - so select the canvas and inside the canvas I'm gonna create a button here so we're just going to be my UI and button select the button and this is going to be I'm gonna name this as load next scene right and before doing any of this let me select the canvas and I'm gonna select this canvas scalar and change this UI scale mode to screen scale with screen size so that's gonna make it a bit you know because that's gonna help you in adjusting your screen layout however you move your size of the screen and yeah I think that's fine and I'm gonna select the scene button and increase the size to let's say 300 by a hundred yep that looks better and I'm gonna snap this to the right bottom corner here I'm not gonna cover up all these basic things about creating buttons and all these things and all I'm just gonna move on with that so because this is mainly focused on switching scenes using a button so I'm just gonna name this select this load neck and just select the text and rename the sass what do we what do we do what we do next scene maybe yeah that looks better so just increase the font size slightly yeah either you can just have this or I'll just have a simple double arrow anything's fine I think this should oh sorry yeah this should do so now we have this basic setup here now what I'm going to do is I'm just going to save the scene so file save scenes and then we need another scene here so I'm just gonna save the same ass safe scene ass so we have two scenes here scene 1 and scene 2 so let me let's just study up things here so just right-click create in your folder and call this scenes and just select these two and just move them inside yeah so we have nice and clean project here so now currently we are in scene two so just in order to differentiate this I'm just gonna select the scan with and panel and just make this a bit darker somewhere like this and I'm gonna select the text and change the text color to some were quite oh yeah so this is going to be mine scene 2 right now we don't have more than two scenes here maybe I'll do one more thing here let's try switching so let me just save this one here and I'm gonna go again and save scene s and under since you want to save this as scene 3 right so let me just change the color a little bit somewhat like this okay I know it's a bit contrasty but so it's fine scene 3 and in this case now I want this particular thing to go to the previous previous scene or probably to my first first scene right so I would prefer going to my first scene so I just change the sense home so which is going to be my first scene one so we're going to switch from scene one to C into and then from scene to scene three and then we're gonna switch back from scene 3 to the home screen which is my scene 1 great so we have three different scenes here let's get on with the script now we go to assets hand right click and creating your folder and we'll call this s scripts right and under scripts I'm gonna create a new C sharp script C sharp and I'm going to name this name disaster load C right yeah let me open up in Visual Studio so we got the new c-sharp script here for loading the scene so first we need to okay I'm just gonna get rid of these things here and I'm gonna get rid off we don't want the start function an update function we're gonna create a separate function here so I'm gonna mention public void and we can just name the function as scene loader and yeah and the functions created now so right now we want to load the scene in the parameters I'm gonna give an integer parameter and I'm gonna mention it as scene index right now so we have the basic function ready so now we have to tell the function what to do so here we have to call the scene management function scene management class so in order to use the scene in management class in unity we need to call the namespace using unity engine dot scene management without that we won't be able to use a scene management function so sorry again scene management class yeah so scene manager dot load scene and I'm gonna mention it as scene index and semicolon so this is the basic function here so let me just minimize it for now and let me go to my scenes come on okay and scene 1 and here let me select my load next scene and what I'm gonna do is I'm going to select this add component and look for even trigger there is one other way to do this that is you can just simply go ahead and use this on click event function but the reason why I am using event trigger is you can use even trigger even in your mobile games so if you think is you're using even trigger it see you in working out both with your Android iOS and whatever report so all these things so an even trigger is created here and select add new even type and I would prefer using pointer up so select pointer up and I'm going to create a new event new list here so click on the plus icon here and you can see that the new event is created here so but here we haven't got any script loaded to the scene yet we have created the script here but we haven't added it to the scene so I'm going to create an empty object here and I'm just going to name this as same manager load scenes crypto scene manager right now get back to my load scene which is my button here load scene button and I'm going to drag and drop the scene manager here and under no function I'm gonna select this load scene and I'm looking for scene loader okay so now when I click on scene loader you can see that it's asking for a number to be mentioned here the reason we are why we're getting a number here is because we have added a integer parameter here and one other important thing which I forgot to mention here is the access modifier public is very important in this case because let me just show you what happens if you don't have access modifier I'm just removing the public and let me go back here and if you go to load scene you could notice that our load scene function is not available now because by default if you don't mention the access modifier in any function in unity by default it's it's been taken as private so we need to mention this as public so that it can be accessed outside the script as well so now if we go and select the scene and go to no function load scene you can see the scene loader is again back now so select it and here it's mentioned as 0 by default in c-sharp the number starts from 0 we'll be addressing the first scene as 0 and the second scene as 1 and the third scene as to and so on so here the we want to when you press this button we want the second scene which is seen to to be loaded which is going to be one of index one and let me do the same thing here let me go back to my scene two and select the load next scene and same thing even trigger a new event type pointer up a new list and again we don't have this you can see that we used our scene manager empty object in our previous scene if we want we can use the same object here as well so let me just save it here and get back to my scene one and you can see this is a scene manager I'm going to create a simple new folder here and I'm going to name this as prefabs and inside prefabs I'm going to just drag and drop this inside so now they've got a prefab here so you can just use the same prefab in how many number of things you want so let me go back to my scene to save it for now and go to prefabs and drag and drop the scene manager here so yeah you've got the scene manager here and go to the canvas load scene and I'm gonna drag and drop this one here and no function load scene and scene loader and now we want to load the next scene which is scene 3 which is of index 2 as I said the first scene will be 0 second scene will be 1 and third scene do be - so right now we want we are in the second scene and we want to know the third third scene so we're using the index 2 here right great so save it and let's just move on to scene 3 here and again I'm going to the prefab drag and drop the prefab here scene manager and load next scene which is my home which is going to be my first scene so just drag ok so we just need to create even trigger add new even type pointer up list and scene manager drag-and-drop no function load scene and scene loader and yeah it's going to be zero so that's it so now let's just save all these things and let me go back to my first scene and let's just play and let's see if it works okay I keep pressing next and it doesn't work for me okay the reason being we haven't added the scenes to the build settings here so you can see we have getting a meta message here and go to the console and you can see a scene with build index one one could not be loaded because it's not there the reason being it's not there in the build settings so go to file build settings and here in build settings you can see there are no scenes available so select all the three scenes which already want to load and just add it there and make sure that you add it in the right order because you can you can notice here that scene one is of index 0 and scene - is of index 1 and scene 3 years of index - if you change the order the scenes are also going to change so add these things and just close it and now go ahead and play it right and then click Next right scene 2 next scene 3 and home see anyone great it works fine so that's it for this session guys so we just saw how to switch scenes between you know different scenes in unity 5 using buttons so I'll just catch you guys with another interesting tutorial and until then bye for now if you liked the video just don't forget to like it and please don't forget to subscribe for the channel and if in case you find it useful don't forget to share it with your friends so that's it for now and see you guys then bye bye
Info
Channel: CG AURA
Views: 67,395
Rating: undefined out of 5
Keywords: Unity5, Unity3D, Unity, Switch between scenes, switch scenes, switch scenes in unity, switch scenes in unity using button, switch scenes in unity 5, change scenes in unity, change between scenes, change scenes, change scenes in unity using UI buttons, switch scenes in unity 3D using C#, UI buttons, UI buttons Unity, unity buttons, Unity5 tutorials, unity tutorials, raggz tutorial
Id: FSt5xrFHaFU
Channel Id: undefined
Length: 13min 33sec (813 seconds)
Published: Thu Aug 24 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.