using System; using TMPro; using UnityEngine; public class GameTimer : MonoBehaviour { TMP_Text label; float timer; float highScore; public bool stopped; public bool showHighScore; const string HighScoreKey = "High Score"; void Awake() { label = GetComponent(); timer = 0f; highScore = PlayerPrefs.GetFloat(HighScoreKey); stopped = true; } void Update() { if (stopped) return; timer += Time.deltaTime; SaveHighScore(); label.text = TimeSpan.FromSeconds(timer) .ToString(@"mm\:ss"); if (showHighScore) label.text += "\nBest Time: " + TimeSpan.FromSeconds(highScore) .ToString(@"mm\:ss"); } public void SaveHighScore() { PlayerPrefs.SetFloat(HighScoreKey, timer); highScore = timer; } public void ResetHighScore() { PlayerPrefs.DeleteKey(HighScoreKey); highScore = 0f; } }