- Home /
Timer script is turning minutes into seconds
the timer is changing minutes into seconds, and seconds into ms for some reason:
function Update() {
timer -= Time.deltaTime;
minutes = timer / 60;
seconds = timer % 60;
now = String.Format("{0:0}:{1:00}", minutes, seconds);
countdownLabel.text = "Starts in: " + now;
}
also i added Time.timeScale = 1.0f; in awake() but the time is running much faster than 1 update per second...
using Time.deltaTime should make the change once per second, as that is normalized time rather than Time.time which is frame-rate dependent
No. Time.deltaTime doesn't change once per second. It changes every frame.
Time.deltaTime is: "The time in seconds it took to complete the last frame (Read Only)."
@FairGamesProductions - your suggestion is worse than the original. It will display 1 $$anonymous$$ute and 5 seconds as 1:5.
Answer by MrSoad · Nov 08, 2014 at 03:49 PM
mcconrad, using Time.deltaTime will not make the change once per second, listen to 767_2 he is right. Time.deltaTime will give you how long it has been between this Update and the last Update, a small fraction of a second. If you want the change to only happen once every second then you should use the InvokeRepeating method(repeating once every second) instead of running this in Update.
Answer by tanoshimi · Nov 08, 2014 at 03:45 PM
When using UnityScript, you really really need to type your variables. Your logic for second/minute calculation only works with integer maths. Try this:
function Update() {
timer -= Time.deltaTime;
var minutes : int = timer / 60;
var seconds : int = timer % 60;
var now : String = String.Format("{0:00}:{1:00}", minutes, seconds);
countdownLabel.text = "Starts in: " + now;
}
i declared all my vars outside the function. here is full:
#pragma strict
var timer: int;
private var $$anonymous$$utes: int;
private var seconds: int;
private var now: String;
private var countdownLabel: UILabel;
function Awake() {
countdownLabel = GameObject.Find("CountdownLabel").GetComponent(UILabel);
Time.timeScale = 1.0f;
}
function Update() {
timer -= Time.deltaTime;
$$anonymous$$utes = timer / 60;
seconds = timer % 60;
now = String.Format("{0:0}:{1:00}", $$anonymous$$utes, seconds);
countdownLabel.text = "Starts in: " + now;
}
Your answer
Follow this Question
Related Questions
countdown/countup timer 1 Answer
How to stop a Countdown Timer? 1 Answer
Timer script not perfectly working 1 Answer