- Home /
Problem with timer script
Hi all i am having a problem where i looked at for the last few days but i really cannot find out what is wrong. I don't have a clue anymore. I already tried to change a lot but nothing helps.
I have a timer scripts which keeps track of the time during the game. In it is a variable named "looptscript" which is changed outside the timerscript to stop the timer when the player is gameover or returns to the menu etc.
The problem is that the script runs but now after 30 seconds instead of counting on to 31, 32, 33 etc. I makes the time 1.30 so it counts on a whole minut.
Below is my timer script. Other scripts read the time stamp it generates and adjust game difficulty to it.
import System;
var startTime: float;
var printtext: String;
var guiTime : float;
var timemins : float;
var timesec : float;
var looptscript : boolean = true;
function Update () {
guiTime += Time.deltaTime;
timemins = guiTime / 60;
timesec = guiTime % 60;
printtext = String.Format ("{0:00}:{1:00}", timemins, timesec);
}
if (looptscript == false) {
guiTime = 0.0;
}
function OnGUI() {
GUI.Label (Rect (10, 10, 100, 20), printtext);
}
Answer by Karsnen_2 · Oct 29, 2012 at 03:06 PM
Try this one.
The Code:
var min : int;
var sec : int;
var fraction : int;
var timecount : float;
var starttime : float;
var timeCounter : GUIText;
function Start ()
{
starttime = Time.time;
}
function Update () {
timecount = Time.time - starttime;
min = (timecount/60f);
sec = (timecount % 60f);
fraction = ((timecount * 10) %10);
timeCounter.text = String.Format("{0:00}:{1:00}:{2:00}",min,sec,fraction);
}
You might want to check out this answer. I am not really sure what is causing the issue for you.
http://answers.unity3d.com/questions/334877/display-time-at-game-over-scene.html
I still don't know what went wrong with the other script, because it was working before. But probably something changed... I took your script and it works like a charm.. thank you $$anonymous$$arsnen!
Your answer
Follow this Question
Related Questions
How to stop a Countdown Timer? 1 Answer
making a timer (00:00) minutes and seconds 10 Answers
Countdown Timer Help About putting 0's 1 Answer
Making a timer 1 Answer