- Home /
reset timer using a GUI button
in my game you have to collect things in a certain amount of time. If you have failed you must re-do the level or if you pass the time will become less
I want to reset timer using a GUI button, should i be using delta instead of time.time as i know this counts from the start of the scene
when i click my GUI button the timer stays at 0 please help :)
this is my script so far
var isPaused : boolean = false;
static var startTime : float; //(in seconds)
static var round : boolean;
var timeRemaining : float; //(in seconds)
var GuiText : GUIText;
function Awake(){
startTime = 10.0;
}
function Update() {
if (!isPaused) { // make sure the timer is not paused DoCountdown(); } }
function timer(){
startTime = 10.0;
isPaused = false;
}
function DoCountdown() {
timeRemaining = startTime - Time.time;
ShowTime();
if (timeRemaining < 0) {
Roundendscript.End = true;
GameController.Over = true;
isPaused = true;
timeRemaining = 0;
TimeIsUp(); }
}
function PauseClock() { isPaused = true; }
function UnpauseClock() { isPaused = false; }
function ShowTime() { var minutes : int;
var seconds : int;
var timeStr : String;
minutes = timeRemaining/60;
seconds = timeRemaining % 60;
timeStr = minutes.ToString() + ":";
timeStr += seconds.ToString("D2");
GuiText.text = timeStr; //display the time to the GUI
}
function TimeIsUp() {
Debug.Log("Time is up!");
}
Answer by mattbenic · Oct 02, 2012 at 12:21 PM
You should be setting your startTime to Time.time and then comparing that to Time.Time in your tests:
var timeRemaining : float;
var startTime : float;
var timeLimit : float; // Set this to whatever you want your limit to be in seconds, so 10 in your case
function Awake(){
resetClock();
}
function resetClock(){
startTime = Time.Time;
isPaused = false;
}
function Update() {
if (!isPaused) {
// make sure the timer is not paused
DoCountdown();
}
}
function DoCountdown() {
var timeElapsed = Time.time - startTime;
timeRemaining = timeLimit - timeElapsed;
ShowTime();
if (timeRemaining < 0) {
Roundendscript.End = true;
GameController.Over = true;
isPaused = true;
timeRemaining = 0;
TimeIsUp();
}
}
Answer by Student Juice · Dec 13, 2012 at 05:30 PM
thanks for your answer but im still having problems:
ullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value) Timer.resetClock () (at Assets/Scripts/Timer.js:15) GameController.OnGUI () (at Assets/Scripts/GameController.js:17)
im calling the fuction from my gui
gameObject.Find("Timer").GetComponent(Timer).resetClock();
but it still doesnt work when removed
start and timeRem are set to 0 in viewport
Your answer
Follow this Question
Related Questions
GUI Help, timer and counter 1 Answer
C# countdown timer 9 Answers
how to create race lap timer, start to finish, display gui? 1 Answer
Counter doesn't reset 1 Answer