Unity - How To Save Game / Data [PlayerPrefs]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when developing a game a crucial component that is needed but often overlooked is the ability to save data on your players device this could be something as simple as saving a high score or whether or not the player would like to mute the game's music maybe you'd even like to store their first name so that you can use it in your storyline in this video I will show you how to save player data using unities built-in component player press in order to save data locally to a player's device it can be easily stored in three ways using player preps player press accepts three formats float integer and string to store a value you would simply write player press dot set and then you would enter your format let's just use int for this example in the first parameter you'd put a reference key like high score and then in the second parameter you would put your integer value then to grab your save value you just do player press get int and then in parentheses you would put high score it's really that simple lastly if you wanted to lead a value just to player prefs dot delete key and insert your reference key in the parentheses there you have it you just learn how to save data for your game now let's look at an example in unity in this scene I have setup a UI text box to enter your name and a UI button to save it right now you can enter your name but if you press save nothing happens so let's create a script that will save the text in the textbox to a player perhaps string let's go ahead and create a script that we can assign to our button let's just label this save name then up at the top let's include Unity engine UI so that we can grab the text from our text box then let's create a public reference for input field and let's label this text box we will use this to grab the text from the text field then let's create a function for when our button is pressed let's just label this click Save button then let's assign a string to player press by doing player prefs dot set string let's reference to name and for our value value let's do text box dot text then let's output to our console by doing debug dot log and writing your name is and let's grab our player press value by using player preps dot get string and let's use our name reference now if we save and go back to unity let's drag our new script onto our camera then let's drag on our input field into our text box inspector so our script knows which input field to grab our text from then on our button let's assign our on click object to our main camera where our script is currently being held on and then for our function let's find our click Save button function that we just created this will execute our click Save button function anytime the button is pressed now if we press play and enter our name and then press save we should see that our name is displayed in our console so if I open up this second scene and press play it doesn't display my name and that's because I didn't program it to do that yet instead let's create a quick script that will modify this text field to display my player perhaps name just like my last script let's create a quick reference for our text field and let's call it name box then in our start function let's assign the name box text to equal our play prefs dot get string and let's use our name reference key then if we drag our text box into the inspector and press play we should now see our text box update to our saved string value on the bottom of the screen you might have noticed that we have a reset button currently if we click on it nothing happens so let's go back into our script and create a function for it what I want to do is create a function so that when the reset button is pressed it deletes the player pressed value and then returns me back to the first scene to do this we must include Unity engine scene management then let's create another function except this time let's label it reset savegame inside the function let's use player preps delete key and then let's include our reference key which is going to be name then underneath let's write scene manager that load scene and let's put sample scene which is the name of our first scene this function will delete our save data and load the first scene again let's assign the on click function to our new reset saved game function so now when we press play we should see our name displayed but when we first press reset it should take us back to the first scene if we go back to our second scene and press play we should see a blank value now in our text box that's because we don't have anything assigned to that value now you should be equipped to write your own code to save what you need for your game [Music] you
Info
Channel: Press Start
Views: 65,614
Rating: undefined out of 5
Keywords: playerprefs, unity, unity save, save game data, unity saving game, unity saving player preferences, unity saving high score, unity saving score, unity playerprefs, unity tutorials, unity tutorial series, brackeys
Id: y7RPVvwjrsA
Channel Id: undefined
Length: 6min 55sec (415 seconds)
Published: Fri Sep 14 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.