Created
October 27, 2023 20:47
-
-
Save TheMehranKhan/cb59546bfe29e97a785a520af2eec572 to your computer and use it in GitHub Desktop.
Keeps track of the player's score and updates a score text UI element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
/// <summary> | |
/// Keeps track of the player's score and updates a score text UI element. | |
/// | |
/// This script can be used to centralize the game's score logic, making it easier to maintain and update. | |
/// | |
/// To use the score manager, simply attach this script to a game object in your scene | |
/// and assign the score text UI element to the `scoreText` public property. | |
/// | |
/// To add points to the player's score, call the `AddScore()` method. The score text UI element will be | |
/// automatically updated with the current score. | |
/// </summary> | |
public class ScoreManager : MonoBehaviour | |
{ | |
/// <summary> | |
/// The score text UI element. | |
/// </summary> | |
public Text scoreText; | |
/// <summary> | |
/// The player's current score. | |
/// </summary> | |
private int score; | |
/// <summary> | |
/// Initializes the score to 0. | |
/// </summary> | |
void Start() | |
{ | |
score = 0; | |
UpdateScoreText(); | |
} | |
/// <summary> | |
/// Adds points to the score. | |
/// </summary> | |
/// <param name="points">The number of points to add.</param> | |
public void AddScore(int points) | |
{ | |
score += points; | |
UpdateScoreText(); | |
} | |
/// <summary> | |
/// Updates the score text UI element with the current score. | |
/// </summary> | |
void UpdateScoreText() | |
{ | |
scoreText.text = "Score: " + score.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment