Hey guys! I need help in making a timer that acts like a stop watch. The time counting ends when you finish the game. I have no idea how to solve this except when using the timeSinceLevelLoad statement. Need help guys. I am on deadline to comply for my graduation rites. >_<
So if im not mistaken you want a timer that starts when you start the level, then ends when its completed. or like a timer that counts down, and u have to beat within time limit.
So here is of you wanted to count from start of a game…
private var timecount = 0;
function Start ()
{
//this will send the Counter function every second
InvokeRepeating("Counter", 1, 1);
}
function Counter ()
{
//This will add 1 second to the timecount variable, so everysecond it will add 1
timecount++;
}
function OnGUI ()
{
//this displays the time
GUI.Label(Rect(10, 10, 150, 50), "Time: " +timecount);
}
Of you could add a boolean to make sure to only start when you want it to, as well as end. This would allow you to do multiple levels within one Unit Scene, and just save each time using PlayerPrefs.
private var timecount = 0;
var timecountEnabled : boolean = false;
function Start ()
{
//this will send the Counter function every second
InvokeRepeating("Counter", 1, 1);
}
function Counter ()
{
//This will add 1 second to the timecount variable, so everysecond it will add 1 ONLY when the timecountEnabled is true
if(timecountEnabled == true0
{
timecount++;
}
}
function OnGUI ()
{
//this displays the time - only when timecountEnabled is true.
if(timecountEnabled == true)
{
GUI.Label(Rect(10, 10, 150, 50), "Time: " +timecount);
}
if(GUI.Button(Rect(180, 10, 30, 50), "Click me to start/stop timer"))
{
//change the timecountEnabled to what it is not. from true to false, and vice versa
timecountEnabled = !timecountEnabled;
}
}
function Update ()
{
//You could do the same thing as the button in here, liek if you hit a collider, you just say the
// timecountEnabled = !timecountEnabled;
}
Just the 1st statement. Just a timer that ends when the level is completed. Because I will need the completed time to determine the grade of performance of the player.
If you wanted a countdown timer, then you would do the same basic thing, but start the variable at a higher number.
private var [color=purple][i]timecount = 120;[/i][/color] //1 minute = 120 seconds
function Start ()
{
//this will send the Counter function every second
InvokeRepeating("Counter", 1, 1);
}
function Counter ()
{
//This will subtract 1 second to the timecount variable, so everysecond it will subtract 1
timecount[color=purple][b]--;[/b][/color] //[b][color=red]<-----[/color][/b]
}
function OnGUI ()
{
//this displays the time
GUI.Label(Rect(10, 10, 150, 50), "Time left: " +timecount);
}
Also one other thing, you could make the time much more complicated and add minutes and what not, I will leave that up to you, but one thing is, to send the counter function at different second intervals you change the following.
function Start ()
{
//this will send the Counter function every second
InvokeRepeating("Counter", 1, 1);
}
This code (from above) to this…
function Start ()
{
//this will send the Counter function every sixty seconds
InvokeRepeating("Counter", 1, [color=orange][b]60[/b][/color]);
}