- Home /
How do I change this countdown timer to M:S
Hi I downloaded this very elegant javascript for a countdown timer which reverts back to menu page when it reaches zero but I can't figure out how to change the countdown from seconds to minutes and seconds i.e. 2 minutes needs to be displayed as 2:00 and not 120. Been looking for ages for an answer so any help would be much appreciated.
var timerText : GUIText;
var roundTime = 60.0;
var thisSecond;
var lastSecond = 0;
function Update () {
thisSecond = Mathf.FloorToInt(Time.time);
if(thisSecond > lastSecond)
{
roundTime -= 1;
timerText.text = roundTime.ToString();
}
if(roundTime < 1)
{
Application.LoadLevel(0);
}
lastSecond = thisSecond;
}
Answer by xandermacleod · Feb 08, 2014 at 12:51 AM
to turn seconds into minutes you do 2 operations. First is for the number of minutes, the second is for the remaining seconds.
To get the minutes first divide your total seconds by 60 then do Mathf.Floor on it's result. i.e. var minutes = Mathf.Floor(totalSeconds / 60);
To get the remaining seconds you do a special type of division that returns the remainder. Instead of doing a division (e.g. 120 / 60) use the percentage symbol (i.e. 120 % 60). i.e. var remainingSeconds = totalSeconds % 60;
hope this helps
Sorry for being thick but where do I put the lines above. If I add them to the variables at the top I get an error "BCE0005:$$anonymous$$ Identifier:'totalSeconds'. I am guessing I have to modify the remaining code to reflect the changes but am not sufficiently advanced at coding to work out where. I apologise as I am a 3D modeller trying to learn coding rather than a coder trying to learn modelling (which is obviously the easier option).
Just wondering if anyone knows how to incorporate the suggested code so that it works without returning an error. I added the two lines at the top and am guessing I also have to alter the body of the code to reflect the changes but do not have the skill. If we can get this code working to show $$anonymous$$utes and seconds it is a very simple and easy solution to a countdown timer. I have searched for something better but this is the best I could find so far. If anyone has a better timer to attach to GUI text I would be grateful.
I haven't tested it so there might be bugs, but you'll want something like this:
public var countdownTotal = 120.0;
public var $$anonymous$$uteText : GUIText;
public var secondText : GUIText;
private var secondsLeft : float;
private var $$anonymous$$utesTextNumber : int;
private var secondsTextNumber : int;
function Update()
{
secondsLeft = countdownTotal - Time.time;
if(secondsLeft <= 0)
{
CountdownHasHitZero();
}
$$anonymous$$utesTextNumber = $$anonymous$$athf.FloorToInt(secondsLeft / 60);
secondsTextNumber = $$anonymous$$athf.FloorToInt(secondsLeft % 60);
$$anonymous$$uteText.text = $$anonymous$$utesTextNumber.ToString();
secondText.text = secondsTextNumber.ToString();
}
function CountdownHasHitZero()
{
Application.LoadLevel(0);
}
you might wanna test to see when it reaches 60 seconds if it shows up as 1:00 or 0:00... if it shows up as zero then let me know and ill adjust it.
Answer by gpvisual · Feb 12, 2014 at 10:18 AM
Actually sorted it myself (or to be correct my son did). If from line 15 the first "if" statement you add the following it works perfectly.
if(thisSecond > lastSecond)
{
roundTime -= 1;
minutes = Mathf.FloorToInt(roundTime/60);
seconds = roundTime % 60;
timerText.text = minutes + ":" + seconds;
}