- Home /
Countdown Timer location
Thanks to the code provided by Phuzzy on the forums, I finally figured out how to create a countdown timer in my game and have it linked to GuiText. However, I am having a problem getting the GuiText to appear where I want it to. I am really new to programming and have only been working with the Lerpz demo so far so I tried to add the code from the StartMenu into the countdown timer that deals with the postion of the title of the game. This of course did not work. I have also tried using the Anchor and Alignment settings in the GUIText, but it is not working. Also, I used the Pixel Offset to get the GUItext at the top of the screen in Unity, but when I go into the webplayer, the timer is missing. I want the code at the top of the screen and centered no matter what the screen size is. Right now, it is centered, but it is sitting right in the middle of the screen. What do I need to do to get it at the top?
Here is the full code for the timer:
//countdownTimer: methods to handle a countdown timer //it is always assumed that there is a guiText item available for the display output
//PRIVATE MEMBERS private var b_timer_active : boolean; //switch to start/stop timer private var f_timer_done; //method to be called when timer runs down private var fl_start_time : float; //start time (in seconds) private var fl_time_left : float; //time left (in seconds)
//PUBLIC METHODS function getFlRemainingTime() { //get the time remaining on the clock return fl_time_left; }
function setTimerDoneAction(f_method_fp) { //set the method to be called when the timer is done f_timer_done = f_method_fp; }
function setTimerState(b_active_fp : boolean) { //set the active state of the timer b_timer_active = b_active_fp; }
function setStartTime(fl_time_fp : float) { //set the starting value for the countdown fl_start_time = fl_time_fp; }
function Update() { if (b_timer_active) { //check to see if the timer is "on" if (!guiText) { //check for an available GUIText component Debug.Log("countdownTimer needs a GUIText component!"); enabled = false; return; } else { doCountdown(); //decrement the time and send value to GUIText for output } } }
//PRIVATE METHODS private function doCountdown() { // if (fl_start_time && b_timer_active) { //make sure that we had a starting time value before conting down fl_time_left = (fl_start_time - Time.timeSinceLevelLoad); fl_time_left = Mathf.Max(0, fl_time_left); //don't let the time fall below 0.0 guiText.text = outReadableTime(fl_time_left); //display the time to the GUI if (fl_time_left == 0.0) { //if time has run out, deactivate the timer and call the followup method b_timer_active = false; if (f_timer_done) { //only call the followup method if we had one f_timer_done(); } } } else { Debug.Log("countdownTimer needs a value set for fl_time_left"); } }
private function outReadableTime(fl_time_fp : float) { //format the floating point seconds to M:S var i_minutes : int; var i_seconds : int; var i_time : int; var s_timetext : String; i_time = fl_time_fp; i_minutes = i_time / 60; i_seconds = i_time % 60; s_timetext = i_minutes.ToString() + ":"; s_timetext = s_timetext + i_seconds.ToString("D2"); return s_timetext; }
Does something need to be added to it that I am missing? Thanks for any help you can provide!
Answer by DaveA · Apr 07, 2011 at 08:37 PM
Check the position of the object containing the GUI text itself. Z=0, x = .5, y = .9 (or .1 I forget)
I got it working! I had to add transform.position.x =.5; transform.position.y=.99; to the Function Update() section and the countdown timer works just fine. Thanks for the help!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Getting an object to spawn on a set time 1 Answer
On Lerpz game making shooting monster 1 Answer
How do I...? [scripting help] 2 Answers
two different collider in one script 1 Answer