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 :slight_smile:

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!");

}

I don't even see an OnGUI function anywhere.

–

2 Answers

2

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(); 
	}
}

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