- Home /
Use timer to remove text
I am trying to get timer to remove GUI text... I have gotten this far and most of it works but I cannot get the timer to actually count down and remove text.
static var textOn : boolean = false; static var textContent : String;
private var textTimer:float ;
function Start() {
textTimer = 0.0;
textOn = false;
guiText.text = "";
}
function Update () {
if(textOn)
{
guiText.enabled = true;
guiText.text = textContent;
var textTimer:float -= 1 * Time.deltaTime;
}
var textTimer:float = 6.0;
if(textTimer >= 5.0 )
{
textOn = false;
guiText.enabled = false;
textTimer = 0.0;
}
}
Answer by Rod-Green · Oct 29, 2011 at 02:58 AM
Seems like you might have some local vs global property issues (scope) as well as others.
try this ( I don't use JS so there might be some issue)
var startTimer:float = 0.0;
var textOffTime:float = 6.0;
function OnEnable() {
startTimer = Time.time;
textOn = true;
guiText.text = "";
}
function Update () {
if(textOn)
{
guiText.enabled = true;
guiText.text = textContent;
}
if(Time.time - startTimer >= textOffTime )
{
textOn = false;
guiText.enabled = false;
}
}
Answer by mikez69 · Oct 29, 2011 at 03:47 PM
Actually it was this line:
var textTimer:float -= 1 * Time.deltaTime;
It should actually read:
var textTimer += Time.deltaTime;
Took a bunch of trial and error but I figured it out. Thanks for the attempt though!
I can't see how that code can possibly work..
if(textOn)
{
guiText.enabled = true;
guiText.text = textContent;
var textTimer:float -= 1 * Time.deltaTime;
}
// textTimer is always 6.0 at this point
var textTimer:float = 6.0;
if(textTimer >= 5.0 ) // This is ALWAYS true
{
textOn = false;
guiText.enabled = false;
textTimer = 0.0;
}
Therefore any calculation done in the (if(textOn)) section is reverted by the next line "var textTimer:float = 6.0" So I guess you must have fixed that issue also.
Your answer
Follow this Question
Related Questions
Accurate timing while using timeScale. 1 Answer
Certain timers run too fast 1 Answer
Timer limits 0 Answers
Total Time Played since Installation 2 Answers
Why will this timer not count down? 1 Answer