Reset the countdown timer

Hello, I built the timer which is triggered by an event in my scene, counts down and as soon as it reaches “0” my player dies - Time.timeScale = 0.0, and appears gui.window with the button “restart”. The button behaves this way, if pushed, it resets all the static variables and loads the same level as I play. But the problem is, right after the “resetting” I spaw in the beginning and immediately see the window of “Game Over” again, with the button “reset” and everything that was explained above. But at this time I push “reset” (its already second time) - it resets everything, seems like… So Im gonna show the GUI script, timer script and the death().

---------TIMER----------

var myTimer: float = 10.0;

var timerOutput: GUIText;

static var timeIsUp = false;

function Update()
{

if(myTimer > 0 && pickUpSample.sampleCntr == 1)
{
	myTimer -= Time.deltaTime;
	var seconds: int = myTimer % 60; // calculate the seconds
	var minutes: int = myTimer / 60; // calculate the minutes
	timerOutput.text = minutes + ":" + seconds;
	
	
}

if(myTimer <= 0)
{
	Debug.Log("Game Over!");
	timeIsUp = true;
}

}

---------GUI box--------

function OnGUI()

{

if(death.isDead == true)

{

    GUI.Box (Rect (291,250,240,115), "
		    You are dead", styleWindowObjectives);
	
	if(GUI.Button (Rect (428,325,75,20), "restart", styleBtn))
	{
		Debug.Log("restart");
		resetTheVariables();
		Time.timeScale = 1.0;
		death.isDead = false;
		Application.LoadLevel("laboratoryG");
	}
	if(GUI.Button (Rect (320,325,75,20), "menu", styleBtn))
	{
		resetTheVariables();
		Application.LoadLevel("mainMenu");
	}
	
	//GUI.Label (Rect (260,300,242,100), " 					It was level1");
}
if(isPaused)
{	
	
	GUI.Box (Rect (291,250,240,115), "
			       Pause", styleWindowObjectives);
	
	GUI.Label (Rect (260,280,242,100), "

			   Press ESC to go back", styleTxt);
}

}

function resetTheVariables()

{

boxGlow.boxTextReady = false;

boxLidOpen.boxIsOpen = false;

death.isDead = false;

diskInsert.diskIsInserted = false;

doorExitBOpen1.doorExitBiSOpen = false;

doorExitEOpen.doorExitEiSOpen = false;

doorLockerOpen.lockerReady = false;

timer.timeIsUp = false;

input1Respond.inputTextEnable = false;

input1Respond.inputReady = false;

inputExit.inputExitText = false;

inputExit.inputExitRespond = false;

keyLockerOpen.keyLockerReady = false;

readerGlow.diskReaderText = false;	

stopHintSuit.suitTextIsReady = false;

triggerS.sTrigger = false;

triggerS.sampleTextHintEnable = false;

triggerE.eTriggerReady = false;

triggerKL.lkTriggerReady = false;

triggerLD.ldTriggerReady = false;

pickUpKey.keyCntr = 0;

pickUpSuit.suitCntr = 0;

diskPickUp.diskCntr = 0;

pickUpSample.sampleCntr = 0;

}

---------death----------

var Camera: GameObject;

static var isDead = false;

function Update ()

{

death();

}

function death()

{

if(timer.timeIsUp == true)

{

	Time.timeScale = 0.0;

	Camera.GetComponent(Tonemapping).enabled = true;

	//Camera.GetComponent(Tonemapping).exposure -= 1;

	isDead = true;

}

}

I simply reload the level if a player tries again after the timer expired.

Anyway in your reset code I do not see where you give the timer a new time,
mytimer = 10;
So your timer is still 0 on reset.

Also if the timer is an adjustable value dat varies every level you may want to adjust the code a bit:

    var timerValue :float = 10;
    private var myTimer :float;
    
    function Awake() :void
    {
    	myTimer = timervalue;
    }
    
    function reset() :void
    {
    	myTimer = timervalue;
    }

And in the reset button in the death script:

	var timerscript = FindObjectOfType(timer); // name of the timer script, caps sensitive!
	timerscript.Reset();
	
	// You can do it all in one line:
	// FindObjectOfType(timer).timerscript.Reset();

Instead of the reset function you could simply call the Awake function (as its the same code) from the death script, but you may get trouble later on when you might add additional code to the awake function.

And it still doesn’t work. I think I put the code correct way… And now, the level doesn’t reset at all after pushing on the GUI.Button “reset”. It just stays frozen on the “game over” screen. “menu” button from the same window works fine, and after loading the level from the menu - it resets everything and works fine. What the heck…? :frowning:
Before I had 3 separate scripts: 1) timer, 2)guiStuff 3)death. Now I combined timer and gui, but the death is still another script. Can you take a look please. Im getting desparate already…

so here is script TIMER.js:

var timerValue: float = 10.0;
var myTimer: float;

var timerOutput: GUIText;
static var timeIsUp = false;

var styleWindowObjectives: GUIStyle;
var styleBtn: GUIStyle;

function Awake()

{

myTimer = timerValue;

}

function Update()

{

if(myTimer > 0 && pickUpSample.sampleCntr == 1)

{

	myTimer -= Time.deltaTime;

	var seconds: int = myTimer % 60; 

	var minutes: int = myTimer / 60; 

	timerOutput.text = minutes + ":" + seconds;

}

if(myTimer <= 0)

{

	Debug.Log("Game Over!");

	timeIsUp = true;

}

}

function OnGUI()

{

if(death.isDead == true)

{

	GUI.Box (Rect (291,250,240,115), "
		    You are dead", styleWindowObjectives);

//something is definitely wrong here

	if(GUI.Button (Rect (428,325,75,20), "restart", styleBtn))

	{

		Application.LoadLevel("laboratoryG");

		Debug.Log("restart");

		resetTheVariables();

		//Time.timeScale = 1.0; ---???? Do I need that..?

		myTimer = timerValue;
		
	}

	if(GUI.Button (Rect (320,325,75,20), "menu", styleBtn))

	{

		resetTheVariables();

		Application.LoadLevel("mainMenu");

	}

}

}

function resetTheVariables()

{

boxGlow.boxTextReady = false;

boxLidOpen.boxIsOpen = false;

death.isDead = false;

diskInsert.diskIsInserted = false;

doorExitBOpen1.doorExitBiSOpen = false;

doorExitEOpen.doorExitEiSOpen = false;

doorLockerOpen.lockerReady = false;

timer.timeIsUp = false;

input1Respond.inputTextEnable = false;

input1Respond.inputReady = false;

inputExit.inputExitText = false;

inputExit.inputExitRespond = false;

keyLockerOpen.keyLockerReady = false;

readerGlow.diskReaderText = false;

stopHintSuit.suitTextIsReady = false;

triggerS.sTrigger = false;

triggerS.sampleTextHintEnable = false;

triggerE.eTriggerReady = false;

triggerKL.lkTriggerReady = false;

triggerLD.ldTriggerReady = false;

pickUpKey.keyCntr = 0;

pickUpSuit.suitCntr = 0;

diskPickUp.diskCntr = 0;

pickUpSample.sampleCntr = 0;

}

//END TIMER.js

//Script DEATH.js:

var Camera: GameObject;

static var isDead = false;

function Update ()

{

death();

}

function death()

{

if(timer.timeIsUp == true)

{

	Time.timeScale = 1.0;

	Camera.GetComponent(Tonemapping).enabled = true;

	isDead = true;

}

//END DEATH.js

Maybe it has something to do with the timeScale? I really have no options… I really hope you will have a bit of clue… :frowning:

Thank you for help!!!