You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Driftology/Assets/CashDisplay.cs

43 lines
855 B
C#

using UnityEngine;
using TMPro;
public class CashDisplay : MonoBehaviour
{
private TextMeshProUGUI cashText;
private void Awake()
{
cashText = GetComponent<TextMeshProUGUI>();
}
private void OnEnable()
{
ShowCash(); // Initial display
GameConstants.OnCashChanged += OnCashChanged; // Subscribe to event
}
private void OnDisable()
{
GameConstants.OnCashChanged -= OnCashChanged; // Unsubscribe to avoid memory leaks
}
private void OnCashChanged(int newCash)
{
ShowCash(newCash);
}
private void ShowCash()
{
ShowCash(GameConstants.Cash);
}
private void ShowCash(int value)
{
Debug.Log("Current Cash: " + value);
if (cashText != null)
{
cashText.text = "$" + value.ToString();
}
}
}