using UnityEngine; using TMPro; // coin is collected public class CoinCollector : MonoBehaviour { public int coinCount = 0; [SerializeField] private TMP_Text coinText; void Start() { // Load the saved coin count (starts at 0 if not set) coinCount = PlayerPrefs.GetInt("Coins", 0); Debug.Log("Loaded Coins: " + coinCount); UpdateUI(); } public void AddCoin() { coinCount++; // Save the updated coin count PlayerPrefs.SetInt("Coins", coinCount); PlayerPrefs.Save(); UpdateUI(); } void UpdateUI() { if (coinText != null) { coinText.text = "" + coinCount; } } }