How to Keep Score in Unity Using Unity Events and Text Mesh Pro

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today we're going to look at how to allow the player to gain points by killing enemies and how to display the score on screen okay we'll start with this scene that continuously spawns enemies that wander around and chase the player the player can also fire bullets and kill the enemies we created this in our 2D top-down shooter Series so take a look if you want to know how it was done alternatively you can download this project by supporting us on patreon all the links you need can be found in the description okay let's start by adding a script to keep track of the score we'll navigate to the scripts folder in the project panel then we'll open the game folder we'll click the plus button and add a new folder we'll call this score we'll open this new folder and add a new script we'll call this score controller we want this component to be assigned to the player so we'll drag the script onto the player in the hierarchy then we'll double click the script to open it in the editor we'll delete the default methods and then add a public integer property to hold the score we'll mark the setter as private so that nothing outside of this script can change the score value directly to allow the score to be increased we'll add a public method called add score this will take the amount to add to the score as a parameter in here we'll simply add the value to the score the final thing to add to this script is an event that will notify other components when the score changes we'll add the unity engine events namespace then we'll add an event called on score changed we'll invoke this event in the add score method so that it fires whenever the score changes okay that's it for this script let's save it and switch back to Unity the next thing we'll do is add to the score every time an enemy is killed we'll navigate to the enemy prefab in the project panel then we'll double click it to open the prefab editor we already have an event on the health controller that fires when the enemy dies we'll create a new script that we can trigger from this event to add to the score let's navigate to the enemy scripts folder and add a new script we'll call this enemy School allocator we'll drag it onto the enemy in the hierarchy to assign it then we'll double click the script to open it in the editor we'll start by deleting the default methods then we'll add a serializable int field for the score to add when the enemy's killed next we need to get access to the score controller of the player we'll add a private field for this then we'll add the awake method in here we'll use the find object of type method to find the score controller this will find the only instance of the score controller as we're making a single player game this will work if we were making a multiplayer game we'd have to do it a bit differently and keep track of which player had killed the enemy okay now we have the score controller we'll create a method to allocate the score in here we'll call the add score method of the score controller and pass in the kill score okay that's it for this script let's save it in Switchback to Unity we'll select the enemy in the hierarchy and set the kill score to 10. then we'll add a new subscriber to the undyed event of the enemy Health controller or drag the enemy into the object slot then we'll select enemy score allocator allocate score from the drop down so now when the enemy dies it'll add 10 points to the score the final thing to do is display the score on the UI let's click back to close the prefab editor then we'll right click on the canvas in the hierarchy and select UI text text mesh Pro when prompted we'll import the text mesh Pro Essentials we'll rename the text object score then we'll click this button to position the text we'll hold down alt and shift and click the top left this will set the anchor points pivot and position to the top left to position the text just below the health bar we'll set the position to 30 on the x-axis and minus 80 on the y-axis we'll set the initial value of the text to score colon 0. next we need to create a script to update the score text whenever the score changes we'll navigate to the score scripts folder and add a new script we'll name this score UI we'll drag this onto the score text object to assign it then we'll double click to open it in the editor we'll start by removing the default methods we'll add the TM pro namespace at the top so we can use the text mesh prototypes then we'll add a private TMP text field for the score text component next we'll add the await method in here we'll get the text component and assign it to the field now we just need to add a public method to update the score we'll take the score controller as a parameter then we'll set the text property of the score text object we'll set it to score colon and then the current score from the score controller by adding the dollar symbol before the string it allows us to embed values using the curly brackets okay let's save this and switch back to Unity we'll select the player in the hierarchy then we'll add a subscriber to the on score changed event we'll drag the score object into the object slot then we'll select score UI update score from the drop down because this method has a parameter for the score controller we now have an additional slot to set we'll drag the player into this slot so now whenever the score changes the method to update the score will be called passing in the score controller of the player okay let's press play to try this out now when we shoot the enemies we can see the score increase in the next video in this series we'll make the enemies randomly drop Health Collectibles when they're killed if you want to be alerted when this one's out then subscribe and click the Bell icon if you have any questions or feedback on this video let us know in the comments a big thank you to all our patrons we really appreciate you helping to support the channel if you'd like to help and also get access to the source code you can find Details In the description thanks guys
Info
Channel: Ketra Games
Views: 3,507
Rating: undefined out of 5
Keywords: Keeping Score Unity, Text Mesh Pro, Unit Events, Unity, Unity Tutorial, unity3d, Unity3d Tutorial, Learn Unity, Indie Game Development, Game Development, Learn Unity3D, Unity 3d, Unity Tutorials, unity 3d Tutorial, Tutorial, Game Development Unity, Unity Game Tutorial, Unity Tutorial for Beginners, Unity Beginner Tutorial
Id: ToYQB8-hDe0
Channel Id: undefined
Length: 9min 30sec (570 seconds)
Published: Mon Sep 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.