-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreUI.cs
More file actions
46 lines (38 loc) · 1.06 KB
/
ScoreUI.cs
File metadata and controls
46 lines (38 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ScoreUI : MonoBehaviour
{
[Header("References")]
[Tooltip("Global score (ScriptableObject)")]
public GlobalScoreSO globalScore;
[Tooltip("Text field for displaying score")]
public TextMeshProUGUI scoreText;
[Header("Formatting")]
[SerializeField] private string prefix = "Score: ";
[SerializeField] private string suffix = "";
private void Start()
{
if (globalScore == null)
{
Debug.LogError("[ScoreUI] globalScore not assigned!");
return;
}
globalScore.OnScoreChanged += UpdateScoreDisplay;
UpdateScoreDisplay(globalScore.Score);
}
private void UpdateScoreDisplay(int newScore)
{
if (scoreText != null)
{
scoreText.text = $"{prefix}{newScore}{suffix}";
}
}
private void OnDestroy()
{
if (globalScore != null)
{
globalScore.OnScoreChanged -= UpdateScoreDisplay;
}
}
}