 Hello everyone is crypto grounds here welcome back to another unity tutorial in this video I'm going to be teaching you how to switch between different input fields by pressing the tab key now this is really useful when it comes to having login screens because you don't want to have to click on a text box type something and you can't press tab to move on to the next text box and you know man I got to click it type click it type and it's just really convenient we can just type tab type tab you know etc so that's what we're going to be doing today and if you enjoyed this video make sure you leave a like helps up the videos and it means a lot subscribe to my channel if you're new around here and turn on the bell for future notifications for videos and live streams let's operate into it so first of all I have three text mesh pro input fields one for username email and password a cool thing that you can do with these is adjust what kind of content type they are so for username it's just going to be a standard single line email we can actually make this just an email address and password well we can make it a password as well and that also just blocks out whatever you type in which is kind of cool so we're going to have a username email and password input and we're just basically get a tab between these three so the first thing we need to do is to create a script well you don't have to create a script you can use a pre-existing one if you'd like but I just create a new one called tab input field so I'm going to open that up all right so to start off we're going to need to import the tm pro namespace so let's do that and we're going to create three input fields okay so I've created one for username email and password next let's create an integer so this will be called input selected and the reason why we're creating this is because we need to determine which input we're currently selected so it's not like it's simple or you press tab and it automatically just selects the next one and stuff like that doesn't work that way we need to actually determine which one we want to select all right next let's create a private void update method so first let's check to see if we're actually pressing on the tab key to do that we're going to make it if statements we're going to be accessing the input class which comes from unity engine and we're going to find get key down and we're going to see if key code dot tab is true so if we are pressing down on the tab key it's going to return true so when we press tab we're going to increment input selected which means we're going to switch from username to email email to password and we're also going to just go back from password to username so let's do that username will be zero email will be one and password will be two so when we just press tab we're going to increment this all right simple as that however if our input selected is greater than two we're just going to set it back to zero so it's just going to loop between these three input fields and what we're going to do after this is select the current input field which we'll get there in a second so next I want to do the same thing but for shift tab so you want to go backwards so I'm going to make a new if statement and I'm going to make it before this one because we're basically checking to see if we're holding down tab and shift so we want to check that first because otherwise it's just going to jump to this every time regardless if we're pressing shift or not so same thing we're checking to see if tab is pressed down and we also want to see if we are currently just holding down on shift so we do get key instead and we're going to put in left shift okay here we go and I'm going to make this an if else statement so I'm going to make that else and in here we're doing the opposite we're just going to subtract one from input selected and if input selected is less than zero we're just going to set it back to two and same thing here we're going to select the input field next I'm going to create a local method inside update and it's going to be called select input field so I'm going to create a switch statement in here to determine which one we're going to select so in here inside of the switch we're going to pass in our input selected so for the first case which will be zero we're going to do username input dot select so each input field has a select method and once you call it it's going to select this one input field it'll deselect all the others and it'll just focus on this one and after that we want to break it now we're going to do the same thing for email and password all right so now we can just replace all of this and add the select input field method to our if statements sweet so now things are going to get messed up here what if we manually select a different input field well input selected isn't going to change so let's just do that real quick let's create three different methods for each of these inputs to determine that it's been selected all right so as you can see here I have three new methods username selected email and password so once the username is selected we're going to change input selected ourselves to zero one and two so these three methods you will need and this should be good to go okay so in unity we need to assign our three input fields username email and password each one of these input fields has four different slots to call methods so we have on value change so whenever we change the text value it'll call method whatever we throw in here on and edit once which is when we exit the input field on select and on deselect so we want to focus on on select so for the username input I'm just going to actually let's just select all of these just to make this a lot easier click on the plus for on select and drag in your script with the the methods that we created in this box and we're going to click no function tab input field and then do username selected so you want to change emails and password though so email will be email selected and password will be password selected okay so now once you select these input fields these methods will be called let's give it a shot okay so currently nothing is selected right now so I'm going to look at this input field so you see it's right now it's zero if we click on the password box it should switch to two oh there we go so I don't know what that flash was I think that was just a text mesh pro thing and this is one zero two one so on you get the point so let's just press tab and see what happens okay cool so now it cycles between all three of these so if we were just to type um crypto my email blah and my password blah you know you can just switch between these three it's very easy now when we do shift tab it's going to go backwards okay cool so you can also do this with enter as well instead of tab if you wanted to all you gotta do is replace the key code um tab with enter so that is up to you you can customize this however you want you can do mouse you can do mouse buttons if you wanted to or something I don't know customize it however you'd like anyways guys I hope this video was helpful and if you learned something new if you enjoyed this video as well make sure you leave a like subscribe for more unity tutorials and content like this turn on the bell for future notifications for videos and live streams anyways hope you guys have a great day or night I'll see you guys in the next one peace