- Home /
GUI Countdown Timer
Hello! I have referenced the countdown timer on this page from previous responses. My issue is how to set it to be activated upon picking up an item. The way its set now, it assigns the variable to Time.time on Awake() and loads it right away onGUI(). I want it to appear on the screen when an item is picked up and begin the countdown from there. Anyone able to help?
You probably need to remove the code that "assigns the variable to Time.time" from the Awake function and add it to the code that gets executed when an item is picked up.
Pretty hard to help if we can't see any of your code...
Sorry. Here's what I have:
var PowerupTimerActive : boolean;
var startTime: int ; private var displaySeconds : int; var count : int; static var textTime : String;
function Update(){
if(PowerupTimerActive){
count -= Time.deltaTime * 1;
//This is the timer display var displaySeconds : int = count % 60;
guiText.text = String.Format ("{0:00}", displaySeconds);
textTime = guiText.text;
if(count <= 0.0){
TimerOff();
}
} }
function TimerOn(){
PowerupTimerActive = true; }
function TimerOff(){
PowerupTimerActive = false; count = startTime; }
The timer basically is working, its just ripping through the count. Ins$$anonymous$$d of being a second timer, it looks more to be miliseconds. What am I doing wrong?
People will be more likely to help if you format your code so that it is readable. The way you have it right now, it's almost impossible to follow without missing something.
I've spent the last 45 $$anonymous$$utes trying to answer your question. Unfortunately I keep reading your code wrong (because the lines are not separated properly).
To format your code you can select it and press the button that looks like a bunch of 0s and 1s (it's the 5th button in the text controls).
Answer by kromenak · Oct 17, 2011 at 06:46 AM
The time you are tracking is in seconds already, just the decimal portion of the value is representing milliseconds. You should be able to get a pretty good seconds timer by just truncating the time value when you display it.
float timerValue; //set this to an initial value on pickup or start, etc.
void Update()
{
if(timerIsActive)
{
timerValue -= Time.deltaTime;
}
guiText.text = (int)timerValue;
}
You can also use string.Format to format it in some way, but if you want to just display the 3...2...1 sort of timer, this should do the basics.
Thanks for your answer...Ive tried manipulating what you displayed, but am not getting any results. I tried your (int)timerValue and it didnt like that; got an error. It was looking for a semicolon.
As you saw in my previous code displayed, I am using a string.Format which does display the 15 seconds, but doesnt count down. I guess Im just confusing myself.
I can guarantee you that this code works because we've used it in our game to create an integer countdown timer:
timerValue -= Time.deltaTime;
float timerValueCeil = $$anonymous$$athf.Ceil(timerValue);
if(timerValueCeil > 0)
{
timerLabel.Text = timerValueCeil.ToString();
}
I'm still not totally sure what you are looking for functionality-wise, but if you set timerValue to the starting value when the object is picked up and then put this in an update loop, it should work.
Answer by GC1983 · Oct 18, 2011 at 12:40 AM
var PowerupTimerActive : boolean;
var startTime: int ; private var displaySeconds : int; var count : int; static var textTime : String;
function Update(){
if(PowerupTimerActive){
count -= Time.deltaTime * 1;
var displaySeconds : int = count % 60;
guiText.text = String.Format ("{0:00}", displaySeconds);
textTime = guiText.text;
if(count <= 0.0){
TimerOff();
}
}
}
function TimerOn(){
PowerupTimerActive = true;
}
function TimerOff(){
PowerupTimerActive = false; count = startTime;
}
Answer by GC1983 · Oct 21, 2011 at 07:34 PM
Can anyone at all figure out my issue? The code I posted above is what I have going.
Sorry. I didnt realize this answer would post above my older post. I reposted the code below as you asked so its legible. Thanks!