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?
3 Answers
3The 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.
– GC1983I 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 = Mathf.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.
– kromenakvar 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;
}
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!
– GC1983
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...
– jahroyPeople 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 minutes 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).
– jahroy