46 lines
1.1 KiB
C#
Raw Permalink Normal View History

// ScoreTextUI.cs
using UnityEngine;
using TMPro;
public class ScoreTextUI : MonoBehaviour
{
[Header("UI")]
public TextMeshProUGUI scoreText;
[Tooltip("Format string; {0} is replaced by the total score")]
public string format = "Score: {0}";
private ClawScoreManager sm;
void OnEnable()
{
sm = ClawScoreManager.Instance ?? FindObjectOfType<ClawScoreManager>();
if (sm == null || scoreText == null)
{
Debug.LogWarning("[ScoreTextUI] Missing references (ScoreManager or TMP).");
return;
}
// set initial text to current total
UpdateText(sm.CurrentScore);
// subscribe to total-changed event
sm.OnScoreChanged += HandleScoreChanged;
}
void OnDisable()
{
if (sm != null) sm.OnScoreChanged -= HandleScoreChanged;
}
private void HandleScoreChanged(int total)
{
UpdateText(total);
}
private void UpdateText(int total)
{
// "N0" adds thousand separators (e.g., 12,345). Change to "D" or plain if you prefer.
scoreText.text = string.Format(format, total.ToString("N0"));
}
}