- Home /
Duplicate Question
Convert Timer to HH:MM:SS
How do I convert my GUI timer into HH:MM:SS
This is my bomb script
var PlantTime = 5.0;
var InSite = false;
var Planting = false;
var Plant = 0;
var BombGO : GameObject;
static var DonePlant = false;
function Start(){
BombGO.SetActiveRecursively(false);
}
function Update (){
if(InSite == true){
if(Input.GetKeyDown("f")){
isPlanting();
}
}
if(Plant == 1){
PlantTime -= Time.deltaTime;
}
if(PlantTime <= 0){
PlantTime = 0;
BombGO.SetActiveRecursively(true);
InSite = false;
Planting = false;
DonePlant = true;
}
if(TruckAnim.IsDone == true){
BombGO.SetActiveRecursively(false);
}
}
function isPlanting(){
Planting = true;
Plant = 1;
}
function OnGUI(){
if(InSite == true){
if(Planting == false){
GUI.Label(new Rect(Screen.width / 2 - 25, Screen.height / 2 + 25, 150, 30), "Press 'F' to plant a bomb.");
}
}
if(Planting == true){
GUI.Label(new Rect(Screen.width / 2, Screen.height / 2, 150, 20), "Plant Time: " +PlantTime);
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
InSite = true;
}
}
Instead of my timer being 3.815446 I just want it to display 3, for example How could I do this please help.
This is not a Unity question, plus it has been answered a couple of times on this list before. Try Googling "unity3d Convert Timer to HH:$$anonymous$$$$anonymous$$:SS."
Answer by clunk47 · Feb 06, 2014 at 10:09 PM
Here's a simple example of a game timer showing in format HH:mm:ss. There are many ways to do this, this is how I prefer. You will obviously need to modify this to your liking.
using UnityEngine;
using System.Collections;
public class TimerExample : MonoBehaviour
{
string s;
void Update()
{
s = System.TimeSpan.FromSeconds((int)Time.timeSinceLevelLoad).ToString();
}
void OnGUI()
{
GUILayout.Label(s);
}
}
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
GUI button not showing up 1 Answer
Problem with the GUISkin on the script 1 Answer
Need help with my script 0 Answers
What Am I Doing Wrong? Variable Names 3 Answers