Created
April 12, 2019 21:32
-
-
Save brismithSFHS/ad16c983aa5be2a02499113988099050 to your computer and use it in GitHub Desktop.
A simple timer for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine.UI; | |
using UnityEngine; | |
public class TimerScript : MonoBehaviour | |
{ | |
//This script should be attached to a UI Text element | |
public Text timerText; | |
public bool keepCounting; | |
private float secondsCount; | |
private int minuteCount; | |
private int hourCount; | |
void Update() | |
{ | |
if (keepCounting) | |
UpdateTimerUI(); | |
} | |
//call this on update | |
public void UpdateTimerUI() | |
{ | |
//set timer UI | |
secondsCount += Time.deltaTime; | |
timerText.text = "Your Time:\n" + hourCount + "h:" + minuteCount + "m:" + (int)secondsCount + "s:" + (int)(((secondsCount % 1) * 100)); | |
if (secondsCount >= 60) | |
{ | |
minuteCount++; | |
secondsCount = 0; | |
} | |
else if (minuteCount >= 60) | |
{ | |
hourCount++; | |
minuteCount = 0; | |
} | |
} | |
} | |
//thanks to http://answers.unity3d.com/questions/64498/time-counter-up.html | |
//and RonnyKibbet for this script |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment