- Home /
Comparing Time
I am creating a puzzle game with 90 levels. Each level Has a stopwatch (Below) which tracks the time. Since sec min ext are separate variables and the time is displayed in a String, I don't know how to compare current time with a saved best time.
var sec : float = 0;
var intsec : int;
var minutes = 0;
var seconds;
var miliseconds : int;
var guiTime;
var time : String;
var startTime;
var timerShow : boolean = false;
var startTimer : boolean = false;
var BestTime = 0;
function Start(){
startTime = Time.time;
}
function Update() {
if(Input.anyKey || startTimer == true){
startTime= startTime-Time.time;
startTimer=true;
sec = sec + Time.deltaTime;
intsec = sec;
miliseconds = (sec - intsec )* 10; // Here you can use 10 instead of 100.
if(intsec < 10){
seconds = "0"+intsec;
}
else{
seconds = intsec;
}
if(intsec >= 60){
minutes++;
sec=0;
}
time = minutes + ":" + seconds + ":" +miliseconds;
}
}
function OnGUI() {
if(Input.anyKey || timerShow == true){
timerShow = true;
GUI.Label (Rect (10, 10, 100, 20), time.ToString());
// GUI.Label (Rect(10,30,100,20), BestTime.ToString());
}
}
Answer by Dave-Carlile · Jun 07, 2015 at 08:26 PM
Why would you separate out the components? Just update a normal datetime variable: timer = timer + Time.deltaTime
- then separate out the components if you need to for some reason for the display or whatever.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200736im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Time.deltaTime not consistent over time 1 Answer
Breathing sound effect using timers 1 Answer
Updating an array indices dynamically 2 Answers
Help With Quickest TimeScore 0 Answers
destroying an object instantiated that came out of spawn point 3 Answers