SAVE SELECTED TOGGLE IN TOGGLE GROUP UNITY (SCRIPTING TUTORIAL)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody in this video we're going to be talking about how to switch between the the selected toggle groups and save them so if i click on toggle 4 and i quit the game and i come back it should be selected on toggle toggle 4 so right now if we hit play we can see that that's not happening so let's say if i go on toggle four i quit and then i play instead of going back to toggle four it stays on toggle one so i'm going to show you how to do this real quick so if you have a folder with your scripts add it onto your uh scripts folder uh i added a folder called tutorial scripts because this is a project that i'm working on and um i have other scripts on there already so just right click go to create c-sharp and then name it whatever you want i'm going to name save selected toggles or say select a toggle and then what you could do is you could go to your toggle group now if you don't know how to make a toggle group and all this i have a video on it i'll leave you a link so you guys can check that out but on your toggle group we could just drag this onto it or what you could also do is add component and search it up save select the toggle and then just click it as you can see now we got two of them so make sure you just got one now uh we're going to open it up so double click on it to open it up now i'm going to explain this real quick since i haven't had any coding or scripting tutorials so using is just the library of code so using unity engine this unity engine has certain library full of code so if you don't have this some of the code won't work so like let's say i erase this and now as you can see this mono behavior turned white and it got a little red error line below it meaning that it needs as you can see right here it says type of namespace cannot be found so the library that um this code is looking for cannot be found so we'll just put back using unity engine and as you can see it is found now public class this is the class you created the script that you created and it is being inherited by mono behavior so this little colon thing means inherited and then after that we would put a little curly braces and then after that there's this void start now start is called before the first frame update so before this is uh called uh the start method is called now there's also other functions like uh void awake and these are all built-in uh functions or methods that unity provides for us so this void awake starts even before the void start so this is good if you want to set your um let's say you have components that you want to find in the scene or if you have a game object that you want to find like your player then it would be best to uh set it right here in the wake but uh let's just get started so for this toggle group to work what we would need is to reference the toggles so as i said we need um to use certain libraries to find certain codes so right now i need to use the toggle group but i won't be able to find it so let's say i put private toggle now i got save selected toggle i got my my script that i made but i don't have the actual topical component or toggle script so what i could do is i could erase these and for us to be able to access this toggle we have to use be using unity engine dot ui so now when i type out toggle as you can see we got a toggle and we got a toggle group right here so i'm gonna click toggle and then we're gonna have a number of toggles so we can make an array which just which is a list of uh toggles so a list of game objects that are toggles or we could just simply just spell out toggle one toggle two and then just continue that for all the toggles that you have so if you got two toggles just one and two if you got five you could do it like that now after you got your toggles set this is private so we won't be able to see it in the inspector so let's say i go in the spectrum i go to my toggle group once it's finished finished compiling so when you do go back just make sure to always control s which is save uh your your file or if you're on mac you could do command as to save it and then once you go back to your toggle group you can see there's no variables nothing showing so this toggle group is also a script and as you can see there's a variable showing and here you don't see any variables so let me show you on the toggle group how this is a script if you go to these little three points you click edit script as you can see this is the code for the toggle group and as you can see you could study this you could learn from this um later on i'll show you how to break down these codes so you guys could kind of understand what all these mean what the summary means what these lines mean these comment lines all this stuff i'll show you how to do it but just to show you um how we can see where is it at we can see allow switch off so if we go over here and we look for allow switch off let me see if i could find it right here so right here you can see it says public bull allow switch off so that's what makes it uh that's why you could see it in the inspector is because it's public now we have it private now another reason to make something public is so they so people could access it access this variable from another script so if i wanted to access this variable from this script let's say i make a private toggle group and then is on or a lot i'll switch and put a lot of switch so if i go over here and i put allow switch dot and then as you can see you could have all these these are all public and if we go to allow switch off right here this is the variable so if we go back this is this right here allow switch off and then we could just set it to whatever we want if we wanted to be true if we wanted to be false and that's why that sets public so if you don't want other scripts to access these toggles set it to private and if you want it to be seen in the inspector all you got to do is put these uh brackets and then just put serialized field so all you do is put serialize field private toggle and then when we save it go back to inspector we should see all four of them so went to saves and as you can see we can see all four of them shown in the spectrum so i'll just drag each one so toggle toggle one toggle two toggle and toggle three now we'll go back to the code and we'll start coding this out so what we're going to do we're not going to need the update function and what we could do is we're going to make a public because we want this to be accessed either in other scripts or in let's say on click events which i'm going to show you how to do right now or on value changes as well so we're going to go to public we're going to put toggle 1 selected and then we're going to put these curly these parentheses and then the curly braces oh and then after the public we gotta put void we can't forget public void so as you can see everything has void void update everything has void so um and then void i think it just means it's going to return something or it's not going to return something so what we're going to do is we're going to put player press which this is the function to save things or the class and then we're going to access the function uh get in or sent it we're gonna set this and we're gonna give it a value or a string it says right here a string so a string always has to have these parentheses and then the name of the value or the key so we're going to put toggle selected is going to be the name of the key now this you can name it anything you want and then we're going to put 0 as the value we're just going to close that off with a semicolon a lot of these things if you haven't noticed you have to use a semicolon to close it off so i could even have it like this as long as i have the semicolon to let the code or the script know that that the line has ended or this part of the code has ended so now what i'm going to do is i'm just going to copy this code and i'm going to paste it four times or three more times and then i'm going to get an error because we have this function or method is named the same thing so i'm just gonna switch this to two three and four and of course you can name these anything you want to and then right here i'm just gonna put one two and three so now on the start function now that we have all these selected if the code is getting too long or too uh cluttered for you you could also just close these right here and then we're gonna put right here i'm gonna open this one just so we could see this what i'm going to do is on the start well actually on the wake method which is a better practice so right here we're just going to change it to awake we're going to put if player press dot get instead of set because now we're not setting the the value we're getting the value so we're gonna do the parentheses and then we're gonna name or we're gonna get the key which is this one and it has to be exactly the same so what i would do is just double click it copy it and then paste it so uh double click it ctrl c and then wherever you want to paste it ctrl v and then with the curly braces or brackets and then open it so what you're gonna do now is we get an error because we just got the integer but we gotta say if it's equal to zero which is the first toggle selected we're gonna put toggle one dot is on which if it's on it will have a check mark if it's off it won't copy and paste it and then we'll set this to two three four and then we'll just set these all to false so i'll just set all these to false and after that we'll just put else if so so for example so if this isn't true then we're going to check this so else if all you got to do is put else if the parentheses and then whatever argument you have which is going to be this we're just going to copy it and we're going to set this to 1 so now if it's 1 it will be toggle 2 that's selected so we'll copy all this and then paste it change this to false and then the rest to true or just this one to true and the rest to false so and then we'll just do this again this time too and then this time number three will be true and we'll just keep going and then right here to finish the else if statements or what you could do is you can make another else if or what you could also do is make an else statement that way you don't have to put the argument anymore so this else statement just means if none of these are true then do this so we're gonna go here click it we're gonna put this one to false and then this one should true so if none of those are or none of these are true this is gonna happen so after all that we are pretty much done with the coding part so let me go over it real quick so serialize field just makes private variables shown in the inspector toggle for you able to be able to access this toggle class you would have to use this unity engine ui namespace now you do not have to use this exactly but let's say i don't have this what i would have to do is i would have to go right here i don't need this code i would have to go right here and actually put unity engine dot ui and then dot toggle and then it would work as well but i would have to do that every time i want to access you know this library so i'm just going to erase this went too far i'm just using unity u1 now so you gotta use the using unity engine to get the toggle and then we set these variables which is all our toggle variables and then an awake function which is called even before the start function we're gonna check if the toggle selected is equal to either zero one two and if it is we're gonna see which ones we set on so set to true or which ones we set off to false so if like as if it's on the check mark will show if it's if it's equal to true the check mark will show if it's equal to false the check mark won't show so um after that there's also we got these functions or methods that we created that have the player press now player press all you got to do is when you want to save a value you set the integer and when you want to get that value you get the integer or you could get a flow or a string but in this case we're getting the integer so after that we name our save you could say save file we name it so the name we give it is toggle selected and uh we named it the same for each one because they're gonna be in the same save file because they're basically the same thing and then we set the value so this value we give it to we give a value of zero this one we give it a value of one two and three and then just remember to control s or command s to sub to save it then we'll go back and we're still not done yet there's just one little thing we got to do is we'll go to the toggle group we'll make sure all our variables aren't in here so if not just you know drag them in and then we'll go to each of our toggles we'll select all of them go down here to the on value changed so usually on value change will look like this there'll be an empty list so you'll just hit the plus button and right here you drag in your script or your code that um so you drag in the game object that the code is sitting on or the script is sitting on so we have our code right here and we want to access this code or script so we'll drag in this toggle group right here right here on the no function we'll go down here and we'll go to our code so these are uh this is like the code for the game object if you wanted to turn on or off the game object if you wanted to you know have a string tag if you wanted to send a message this is for the transform uh if you wanted to look at if this is toggle group if you want to switch something from the toggle group and then uh there's safe selected this is the one we made and these are these usually come with it and then we also have ours this is all uh in abc order or it's supposed to be an abc order at least the the methods are so from here down boil down or broadcast message down there abc order so for our the first one we'll hit toggle one for the second one we'll hit toggle two is selected third one and just you know continue doing that toggle three fourth one toggle four after that you are completely done now if you hit play say we go to toggle three we exit now we go back toggle three is still clicked so let's say we go to toggle two go back i'm gonna move my mouse right over here and toggle two is selected so just to show you guys if this script wasn't here if i remove it and i go to toggle three i close it and go back toggle three won't get selected again so as you can see that's it for this video if you guys enjoyed this video hit the like button if you guys want to see more videos like this hit that like button hit that subscribe button if you guys need help let me know this was actually um asked by a viewer he didn't really ask me to make a video but um i wanted to make a video for anybody that didn't know how to do this so uh just leave a comment if you guys want to see a special video or certain video just let me know once again thank you
Info
Channel: RoBust Games
Views: 6,325
Rating: undefined out of 5
Keywords: SaveToggleSelectedUnity, SaveToggleUnity, ToggleSelectedSavedUnity, ToggleSaveUnity, SaveToggleClickedUnity, SaveToggleChosen
Id: qaukss7qRWE
Channel Id: undefined
Length: 16min 41sec (1001 seconds)
Published: Sun Apr 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.