Points counter, HIGH SCORE and display UI in your game - Score points Unity tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

A simple yet important one for everyone who starts developing Unity games.
Would love to hear your opinions!

👍︎︎ 2 👤︎︎ u/bionicl333 📅︎︎ Jan 19 2021 🗫︎ replies

This post appears to be a direct link to a video.

As a reminder, please note that posting footage of a game in a standalone thread to request feedback or show off your work is against the rules of /r/gamedev. That content would be more appropriate as a comment in the next Screenshot Saturday (or a more fitting weekly thread), where you'll have the opportunity to share 2-way feedback with others.

/r/gamedev puts an emphasis on knowledge sharing. If you want to make a standalone post about your game, make sure it's informative and geared specifically towards other developers.

Please check out the following resources for more information:

Weekly Threads 101: Making Good Use of /r/gamedev

Posting about your projects on /r/gamedev (Guide)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

👍︎︎ 1 👤︎︎ u/AutoModerator 📅︎︎ Jan 19 2021 🗫︎ replies
Captions
[Music] so basically every arcade game has some kind of score you will get it by collecting coins clearing the whole map or by surviving the longest time in this video i'll show you how to count the score in your game how to keep the highest score of the player and also how to create a nice ui for that let's go so i've created a very simple level using free assets when i play it you can see that our character can collect diamonds but you can't see any score at the top so let's fix that we will start by creating a simple ui with our score counter right click in the hierarchy and select ui text that will create for us an event system that is needed for any ui buttons canvas that is drawn on the top of our game and the text itself great to preview how it will look on the screen double click on the canvas this white line represents the borders of our screen and in fact if we go to the game window you can see our text in the same position now let's put the text at the top of the screen to do that make sure you have a rack tool selected and move our text to the top to make sure it will be stick to the top of the screen no matter what resolution is active click on this anchor presets button and select top center as you can see after that change everything is placed correctly now let's look at the text itself for now i will give it an example value like 10 points then time for the font please don't use arial you can easily import other phones to unity simply open google phone's website choose the one you like and click download family font downloaded like this simply drag to the unity assets folder and it should be immediately selectable here in the font menu next font size let's say 18 alignment center middle and the blue color for example as you can see the score text already looks great let's quickly duplicate it move it a bit down and this time set the value to high score 20. this one doesn't need to be as big so i'll change its size to 8 and the color to a bit darker blue looks perfect time for some coding first click on the canvas add component and create a new script called score manager open it here we'll need to link our ui text but before we can do that let's add at the top unity ui dependency type using unity engine dot ui then here at the top of the class type public text score text and public text high score text we'll also need to add two variables in score equals zero and int high score equals also zero all of that to keep track of the current points then here in the start method we like to reset the counter so type score text dot text equals score dot tostring plus points for the high score text it will be simply high score dot text equals high score colon plus high score dot tostring now save the script and in unity attach both text to the corresponding fields by simply dragging them from the hierarchy run the game and you can see that points and high score text are reseted but still if our character jumps on the diamonds they simply disappear without giving us any points let's fix that here on the right i have a method that is triggered when the player collides with the diamond our goal is to send a message to the score manager and add some points let's set it up in the score manager i'll create a new method let's type public void r point open brackets here we need to do two actions first let's change our score by one type score plus equals one after that we need to update the ui by simply copying that line here the next challenge will be to trigger that method from the diamond script to do that we need to create an instance that would basically allow us to access methods from the score manager in any script in the score manager at the very top type public static score manager instance then let's create an awake method void awake open brackets and here type instance equals this that means that at the very start of our game even before the start method we'll set an instance of this manager that way here in the on player enter we can type score manager that instance that add point okay time to test everything out save both scripts go back to inti and press play now when our character will get the diamond we'll see one point added the last thing we're missing is saving the high score i would suggest using a player preps a built-in way to store data for your game here in the add point method we'll type playerpreps dot setint and passing the name of our int let's call it simply high score and then the value score variable just before that we also need to create a simple if statement if high score is smaller than score then run our player prefs method without that even the lower scores would be saved as a high score then here in the start method just before setting up the text type highscore equals playerpreps.getint and request the variable of the same name in my case high score you can also specify here the default value if nothing has been saved yet so type just zero that's it when we'll go back to unity press play get some diamonds and die you can see that the high score is saved what's more even if i would restart the game you can see that the score is preserved if you are interested in creating a simple online leaderboard for your game check out this video and don't forget to subscribe to coco code for more unity tutorials see you soon [Music]
Info
Channel: Coco Code
Views: 19,002
Rating: undefined out of 5
Keywords: unity, cococode, bionicl code, bionicl, unity3d, Maciej maj, tutorial, guide, coco code, highscore, high score, best score, top score, score, points, scoring, unity scoring, unity points, unity add points, coins, points ui, ui with points, unity coins, unity highscore, unity leaderboard, tetris points, Mario points, Mario coins, Mario highscore, count points in Unity, unity collect coins, collect points, scores, ui in Unity, Unity gui, Unity ui, score counter
Id: YUcvy9PHeXs
Channel Id: undefined
Length: 8min 5sec (485 seconds)
Published: Tue Jan 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.