- Home /
Countdown/timer in Javascript
I'm basically trying to make an image fill up the whole screen, about 5 minutes after the game started, (in Javascript). I am a complete newbie to Javascript, so i have no idea how to do it. Help?
Answer by Noah-1 · Oct 11, 2012 at 05:12 PM
Its simple, try something like this:
//timer vars
var startTime;
var timer1:int;
var texture1: GUITexture;
function Update(){TimerStart();}
function TimerStart(){
startTime = Time.time; //time starter
timer1 = Time.time; //Set time
if(timer1 > 5){ //<-------the number here are the seconds you want
Debug.Log ("Time is over, render texture now");
//Here we just enable a GUI texture on the screen (create a GUItexture and disable it)
texture1.enabled = true;
}
}
Basically you just wait for the time you want and then enable a texture that is already on the screen.
Answer by PulseLight · Oct 11, 2012 at 07:32 PM
5 minutes is 60*5 seconds so:
if (Time.timeSinceLevelLoad >= 60*5)
{
//load the image here
}
also if you want the counter to show on the screen you can use this:
function OnGUI() { GUI.Box(Rect(0,0,100,30),"Time = "+$$anonymous$$athf.FloorToInt(Time.timeSinceLevelLoad)); }
I'm converting the Time.timeSinceLevelLoad to Int using $$anonymous$$athf.FloorToInt so it won't show with decimals, just seconds.
Being new to scripting what would the display timer code look like to get the timer to appear in a standard $$anonymous$$ute and second time format 0:00 ?
Answer by kmeboe · Oct 11, 2012 at 03:50 PM
You can delay the call of a method for a certain amount of time by using Invoke: http://answers.unity3d.com/questions/24799/time-function-in-javascript.html
To display an image, use GUI.DrawTexture: http://docs.unity3d.com/Documentation/ScriptReference/GUI.DrawTexture.html
Good luck!
You're welcome! Don't forget to mark the question as answered.
How would one reset the timer to start counting up again once it had reached it target?
Your answer
Follow this Question
Related Questions
Mini Game Timer - Unity 2017 0 Answers
How to make a simple battery? 0 Answers
Countdown 2 Answers
How to Display Map 1 Answer