Question by
Benjamin177 · Jul 12, 2021 at 12:55 AM ·
unity 5timerunity 4how
How save playerprefs timer
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using TMPro;
public class PlayTimerMenu : MonoBehaviour { public float timeRemaining = 10; public bool timerIsRunning = false; public TextMeshProUGUI timeText;
private void Start()
{
// Starts the timer automatically
timerIsRunning = true;
}
void Update()
{
if (timerIsRunning)
{
if (timeRemaining > 0)
{
timeRemaining -= Time.deltaTime;
DisplayTime(timeRemaining);
}
else
{
Debug.Log("Time has run out!");
timeRemaining = 0;
timerIsRunning = false;
}
}
}
void DisplayTime(float timeToDisplay)
{
timeToDisplay += 30;
//float hours = Mathf.FloorToInt(timeToDisplay * 60);
float minutes = Mathf.FloorToInt(timeToDisplay / 60);
float seconds = Mathf.FloorToInt(timeToDisplay % 60);
timeText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
}
Comment