- Home /
Question by
twoface262 · Jun 28, 2011 at 07:09 PM ·
guitexttimerguitext
GUIText not working properly with a timer.
I'm trying to make a Timer Via GUIText heres my code that I attached to the GUI Text
private var startTime : int = 15 ; // start time in minutes.
private var passedTime ;
private var minutes : int ;
private var seconds : int ;
function Awake()
{
// Adjusting time from minutes to seconds.
startTime = startTime * 60;
}
function Update()
{
var theTime : String ;
var timer ;
timer = startTime ;
passedTime = timer - Time.time ;
minutes = passedTime / 60 ;
seconds = passedTime % 60 ;
theTime = String.Format ("{0:00}:{1:00}", minutes, seconds);
}
but When I go into debug mode the text just says "GUI Text" What Am I doing wrong? also I got the code from a forum thread that I read.
Comment
Answer by DaveA · Jun 28, 2011 at 07:42 PM
You are never assigning theTime to the GUIText's text string. Either make the GUIText a public variable and assign it with the Inspector, then set it, or use GameObject.Find in Startup to get a handle on it.
public var myText : GUIText; // set in inspector
....
myText.text = theTime;
}
BTW: You are subtracting the current time from start time, which will always be negative, try reversing that.