Skip to content

Instantly share code, notes, and snippets.

@brismithSFHS
Created April 12, 2019 21:32
Show Gist options
  • Save brismithSFHS/ad16c983aa5be2a02499113988099050 to your computer and use it in GitHub Desktop.
Save brismithSFHS/ad16c983aa5be2a02499113988099050 to your computer and use it in GitHub Desktop.
A simple timer for Unity
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