Unity Tutorial - Simple Character Selection System

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome to another unity video in this tutorial i'm going to be showing you how to make a super simple character selection screen and how to save that selection for a new level that you will load so here we have our little capsule guy and you guys you can see we can change the character we could go from red to green to blue in both directions and let's say we want to play as the green character we hit play and we're on a new level it's as you're playing as player green and as you can see we really have the green capsule let's see how this works start this off we're going to take a look at the character selection screen and over here we have a character's game object which has a character selection component attached to it and it has three child game objects which are just the prefabs of the players so that we can show the player what their character will look like once they are satisfied with their selection now we also have these references over here in the character selection script but we're gonna go into a bit more detail later on for now let's stick to the scene another thing we have in this scene is a ui canvas with three buttons so it has the next previous and star game button and all of these reference the character selection ripped right over here and called their respective functions inside of that script let's go to the games game scene really quickly we don't have much here we just have a simple plane and a game manager with a load character script it has a reference to three different prefabs that we can spawn player from and it has a reference to the spawn point as well as the text that says what care player character we have chosen so now that we have seen how this is set up let's go to visual studio code and see how the code works in our character selection script we have three methods and two public variables the first public variable is the characters variable and if you remember from the scene view we have referenced all of our characters in our scene to this array so that we can cycle through them and we also have an index which is called selected character we have type integer and this one will store the information of which character we have actually selected let's go to the first method which is the next character method in this method once the user clicks the button it will be called and the characters of selective character which is the currently selected one will be set to uh not active so set active false this will mean it will not be visible in the scene the next thing it does it increases the index or the selected character variable by one and then mods it by the amount of characters now we do this so that we can go through cycles so if we are on two which is the last index in our array and if we increased by one it's going to get modded by three in our case and then it's going to go back to zero thus creating the cycle once we have increased our index variable or the select character variable as we have called it here we simply set the new selected character to true the previous character method does the exact same thing but just in reverse it checks if we have fallen below zero and then just adds three therefore looping from the last index in this case that would be two start game method is a method that's called by the start game button now once it is called we have a player pref and player prefs are a simple way in which unity allows you to save certain data and transfer it between scenes without having complications with writing files on your own so they have done this for you so in this case we have the player prefs we set an integer because the we need a integer type variable because the variable we'll be saving is called the select character and it is an integer now we set the integer for the selected character variable now keep in mind you can call this anything i have just called it the exact same way as the variable here itself just so the code can be cleaner so we save the selected character player pref and we put the value of it to the selected character variable once that is done we simply load the next scene in our load character script we have three variables and one method which is the start method that's called by unity now once the start method is called we create a new variable called a select character and we get the integer of the same name that we have saved it previously so it's selected character and now once we have gotten this integer we have successfully loaded the index of the character that we want to use and as you can see we have a new game object it's called prefab and we have we set it to the character prefabs from the selected characters now the character prefabs is the array which stores the reference to all of the possible prefabs that player can choose from once that is done we create a new clone and we do it by instantiating the prefab at a spawn point with its own rotation and once that is done we simply set the label to be the name of the playbook character we have chosen so in our demo case it was player green and that is pretty much it if you have any questions or criticisms feel free to leave them in the comments below as always i do appreciate a like and subscribe it helps me out a lot and i hope to see you all guys in the next video goodbye
Info
Channel: RumpledCode
Views: 31,625
Rating: undefined out of 5
Keywords: unity, unity3d, input, new, tutorial, video, c#, programming, game development, gamedev, free, character, selection, sellection, simple, system, playerprefs
Id: 3qlRgICRoeA
Channel Id: undefined
Length: 5min 30sec (330 seconds)
Published: Fri Sep 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.