How to make a HIGH SCORE in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

It grinds my gears to see people use playerprefs for things far aren't player preferences. Learn to serialize and write files for your target platform.

👍︎︎ 5 👤︎︎ u/StackOfCups 📅︎︎ Mar 30 2017 🗫︎ replies

Brackeys makes great tutorials

👍︎︎ 4 👤︎︎ u/pklokgieters 📅︎︎ Mar 30 2017 🗫︎ replies

I was searching exactly this about 8 hours ago. Why can't you mind read faster?

👍︎︎ 2 👤︎︎ u/Ravery-net 📅︎︎ Mar 30 2017 🗫︎ replies
Captions
hello everyone and welcome to this video on creating a high score for your game in this video we are going to explore some simple techniques for storing data on your system to do that we'll be using unity building player prefs something that's pretty easy to use but can come in really really handy all right let's get started so as you can see I've set up this test scene with a few UI pieces first up we have the road dice button whenever you press this we want the number to change to a random number between 1 and 6 we also have the high score which is going to display the largest number that we have so far so whenever our score is greater than the high score we'll set our high score equal to that number and then finally we have a button for resetting our high score so let's make these things actually do something let's begin by creating an empty object let's reset the transform and call this one dice we also add a component to this and call that dice as well I'm going to choose c-sharp and they create an ADD let's double click it to open it up in Visual Studio let's remove the two using tags up here and both the methods so the first thing we need to do is change our score text to do that we need a reference to the text object and it's using this unity GUI system so remember whenever we code for the unity GUI we need to go up here and say using unity engine UI we're now able to make a variable of type text and we'll call it our score of course you need to make sure that this is marked as public and this way when we save it and go back into unity we can take this score object and simply drag it in now in order to update this we need to make a method let's go void let's call it something like update score or roll dice we can then get a random number we do that by going random range and the number we want to get is between 1 and this is inclusive and then 7 and this is exclusive this way we can get the numbers 1 2 3 4 5 & 6 but not the number 7 we can then store this in a temporary variable so let's write integer and let's give this variable a name say let's just call it number so we're now creating an integer variable code number and giving it a random number between 1 and 6 we can then set score text equal to that number however there is one slight problem with this and that is that our number is currently an integer whereas our text is a string so we need to convert from one data type to another luckily converting to strings is really see all we need to do is write dot to string and then to parentheses and this function will go ahead and convert the number into a string so everything adds up we now save this heading to unity and of course nothing is going to happen so far because we are not actually calling the function we've made the function but we need to also trigger it to do that we use a button event let's find our roll dice button and this is just an ordinary button we go ahead and add an on-click event by hitting the plus we drag in the dice where script is sitting then if we go here and select a function we can go under the die script and select one but you can see that our function is not currently visible that's because we also need to mark the function as public let's go into visual studio and insert public again save that and when we now go into unity we can see that under dice there's now a function called Road dice alright so when we hit play we are now able to roll the dice and get a random number and displayed on this screen but how do we store this as a high score to do that we use what is called player press player perhaps it's a data civilization tool built into unity that means that it's a way for unity to store pieces of data somewhere on our system that means the during our game we can set certain values and then load them back in at a later time and so we can have data persist from one game play session to another as long as it's on the same computer taking advantage of player press it's actually really easy to set a number we go player press dot set integer note that you can also set floats or strings but we'll set integer and this is a function that takes in two values the first one is a key this is simply a string that makes the name of the variable so in our case we could call it something like high score we then write comma and feed it the value in our case the value is going to be number whatever number we rolled if we then want it at a later point to get this value we use player proofs get an example of this could be at the beginning of our game we could do this in the start method which is called whenever we start the game we could display our high score so of course we need a reference to a high score text object so let's create another public text and call this one high score and then in the start method we'll set high score dot text equal to and then we use player press dot get int and as the key we need to write the exact same as we did down here or else it's going to work so I'm just going to copy it and that's all we need to do only of course this is again returning an integer and high score text is requiring a string so we need to again follow this up with dot to string so what we should see now is that whenever we roll the dice we're going to display a random number and store that number on the computer as high score then the next time we start the game we're going to set a high score text equal to that high score number but of course the first time we start our game that isn't going to be a high score stored and so we need to give this some kind of default value and that's really easy all we do inside of a get in method is write a comma and then the default value we want and we're just going to set that to zero let's save this and see if it works of course make sure to select the dice and drag in the high score text to swap so we can play we can see that at first our high score sets itself to zero we then roll the dice and our high score is not going to update yet but if we then exit play mode and play again it's going to remember that value but there are a few problems with this first off we want the high score to update as soon as we roll the dice but we also only wanted to update if we get a value that's larger right now if we roll the dice and get say the value 2 that's what it's going to remember so if we go back here and hit play it should now display 2 and of course we never want a high score to decrease to fix this again we go into visual studio we go down here to where it sets our high score and we simply check if the number we rolled is bigger than our current high score to do that rewrite if then number and we want to check if that's greater than and then we get a high score so we'd write player press and the key is high school and then again a default value of zero and that's it so if this if statement is true it means that we've beaten our high score and so we can go ahead and set it to the new number we could also add some kind of effect here congratulating the player on beating the score finally we probably also want to update the high score right away and not wait until we reboot the game to do that we just set high score equal to the number we wrote again we only want to do this if we roll the greater number and again remember to put two strings here so if we now save and hit back into unity our high score system should actually be complete so currently our high score is two if we roll the dice and get a six you can see it updating but if you roll the dice again and get a one it stays at six and if we exit the game and play it again we can see that our high score still remembers the six but what if we want to reset our high score this is really really handy when debugging but sometimes you also want players to be able to reset their settings and start over doing that is really really simple all we need to do is go in and add another function we'll also make this a public voice so we can trigger it using the button and we'll call it something like reset we can then open and close some curly brackets and in here we'll write player press Delete and here we have the possibility of deleting a specific key in our case we would go in and say delete key and then high score and this would work just fine or we can delete all of the save settings of course use this with caution if you're also storing other stuff like player usernames progress data or anything else it will be deleted but I like living on the edge so I'm just going to write the lead all let's save this and in unity we now need to add this to the onclick event so let's go into the reset button let's add an on click event let's drag in our dice and this time as the function we are not going to select row dice what we set and so when we hit play we can still roll the dice and everything but as soon as we hit reset it's going to reset our high score of course we can't see this because we are updating the text object but if we go out of play mode and go back in we can see that it sets itself back to zero and so all we need to do is just add high score text equals and then we'll write a zero here of course the quotation marks are still there because we want it to be a string now we can actually visually see that it resets that said play roll the dice reset the high score roll it again and you can see that now working let's pretty much it for this video if you enjoyed it make sure to subscribe so you don't miss a future episode also remember that you can easily expand upon the system to turn it into a full-featured scoreboard thanks for watching and I will see you in the next video [Music] thanks of the awesome people who donated in February and a special thanks to Derek huge jerk face filmography James Calhoun and Jason Lotito if you want to become a picture in yourself you can do so a pitching becomes - brekkie thanks a lot guys
Info
Channel: Brackeys
Views: 331,858
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, Highscore, High, score, point, points, save, data, player, prefs, serialization, serialisation, get, set, load, local, system, sync
Id: vZU51tbgMXk
Channel Id: undefined
Length: 8min 47sec (527 seconds)
Published: Wed Mar 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.