Level selection in your Unity game | Unity tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you'll learn how to create a level selection ui and logic that will transfer players to a different scene because having multiple scenes might not be the best idea for your project i will also show you how to pass parameters between them and have only one game scene let's go firstly let's start by creating a beautiful user interface in unity here is my level selection scene currently it's very empty so let's change that if you'd like to follow along simply download free project files link in the description i'll start by creating a new button that will navigate us to the game scene so here in the hierarchy right click on a canvas and select ui button rename it to level selector now i will resize this button to give it 100 pixels width and height then a bit of styling i will change its source image to rounded rectangle and give it this lovely blue color you can find in the background if you don't know how to create rounded rectangles in utui check out this video of mine that details everything step by step lastly let's work on that text i will change its content to just one change font to match our game style and make it a bit bigger perfect currently if i press play our button is already interactive but it doesn't do much so let's write some code click on the button in the hierarchy in the inspector click add component and type the name of our new script i will call it level selector open it here after the press of a button we'd like to open a new scene to do that we need to use unity scene manager at the very top type using utengine.scene management then let's replace the update method with the new custom one that will open a different unity scene type public void open scene and inside of it type scene manager dot load scene and pass in the name of the scene you'd like to navigate in my case i already configured a few example scenes to create scene you simply right click in project assets and select create scene then name it accordingly in my case i like to transfer to this level 1 scene so let's copy its name and paste it here as the load scene argument save the file go back to unity and the last thing before testing will be to connect our button on click event with the newly created script and select level selector open scene i will now press play and click the button as you can see there is an error saying scene level 1 couldn't be loaded because it has not been added to the build settings let's quickly fix that by going to file build settings and in this section since to build let's drag in all of our levels and our level selection at the very beginning basically without that unity doesn't know which scenes will be included in the final game now if we'll try that once more you can see that we can easily navigate to a different scene now let's duplicate that example to create buttons for all of our 8 scenes first step to keep our project tidy will be to create a prefab with our level selector button in assets create a new folder called prefabs and then simply drag to it our button that way any modification to our prefab will also modify all of the level selection buttons on the scene time to duplicate buttons if i click on level selector and press ctrl or command d this button will be duplicated but moving all of the 8 buttons manually seems like a nightmare so i have a better solution for that grid right click in the hierarchy select create empty rename it to grid resize it to the area where you'd like to see all of your buttons and finally click add component grid layout group now every button that will be moved inside of it will automatically fit into the nice grid of buttons i will do a few tweaks firstly let's move this grid a bit down then change constraint to fixed column count of 4 change child alignment to upper center and finally tweak spacing in both axis to make our grid more pleasant if you like to learn more about grids check out this video now everything looks great but if i start the game you can see that each button still navigates us to the same level let's fix that go to our level selector script because each button should navigate us to a different scene we need to create a variable that will keep track of that here under class declaration type public int level then in the open scene method we can remove this fixed number 1 and type plus level dot to string that way the script will autofill the name of the scene based on the defined variable please make sure that there is still a space here after the level word or of course modify it based on your scene's structure save script now a bit of a slow process in unity for each button modify the level variable to reflect which level we should navigate into if i now press play and click the third button for example you can see that we are being navigated to the third scene of course there is still one important thing missing updating numbers you can do that manually but here is a quicker and better way in our script at the very top type using unityengine.ui and then under the level verbal type public text level text and finally in the start method type level text text equals level that to string that way at the beginning of the game all texts will be automatically replaced by the level number save the script and in unity double click on the previously created prefab and drag the text object into the text field in our script that way it will fill that out in every button we already have on the scene now press play and here you have it automatically assigned level buttons that will navigate you to the correct level in the game at the end i would like to show you a concept of the universal scene so let's say that you don't want to have multiple scenes but only one that changes based on the selected level here it is a simple scene that should change level number and background based on the selected option currently if i press play neither of that happens to do that we need to pass in the selected level number between scenes let's open our level selector script tied to the buttons on the previous scene and create a new variable that will coexist between scenes public static int selected level this variable should be assigned just before changing the scene so here simply type selected level equals level lastly we no longer need to switch to a different scene each time we can just say scene manager load scene universal level which is the name of my universal scene found in the assets save it and just as before make sure to add this universal level scene to the build settings in unity now move on to the universal level scene here on canvas i already created a script called universal level manager double click to open it let's create a start method void start and inside of it type int level equals level selector dot selected level lastly debug.log with the parameter of level normally we wouldn't be able to access this variable but because it is static it is available between all objects save it and let's preview our game from the very beginning i click the third scene for example and after the scene switch you can see in our console number of the selected level now let's move this idea a bit further and change scene elements based on the selected level in universal level manager create three new variables public array of sprites called backgrounds public image background and public text text to make them work you will also need using utengine.ui at the very top then in the start method type text dot text equals level plus level dot to string and background dot sprite equals backgrounds with the index of level -1 that's because array indexes start from 0 but our levels start from 1 and that's it for scripting now in unity on the universal level assign level text and the background image to our script and create an array with 8 options with different images and that's it save the game go back to level selector scene and press play now every time a different level text is shown and the background image changes accordingly check out the videos show on the screen to learn more about unity as always thanks a lot to my awesome patrons that support this channel see you soon
Info
Channel: Coco Code
Views: 52,909
Rating: undefined out of 5
Keywords: unity, cococode, bionicl code, bionicl, unity3d, Maciej maj, tutorial, guide, playfab, coco code, unity scene, scene management, level selection, variables between scenes, selected level, Unity UI, basic, simple, select level tutorial, unity select level
Id: YAHFnF2MRsE
Channel Id: undefined
Length: 11min 8sec (668 seconds)
Published: Wed May 25 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.