- Home /
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), "\n\t\t\t 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), " \t\t\t\t\tIt was level1");
}
if(isPaused)
{
GUI.Box (Rect (291,250,240,115), "\n\t\t\t\t Pause", styleWindowObjectives);
GUI.Label (Rect (260,280,242,100), "\n\n\t\t\t\t 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;
}
}
Answer by Bluestrike · Feb 16, 2013 at 10:12 AM
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.
Can u please advice me what exactly do i need to include/change in which part of the code? the var myTimer is set to 10 only in the script, actually it is changed in the inspector to 60. Do I need to assign a new value to this variable? If yes, where do i need to accomplish that?
..Thank you
Answer by sebbo1473 · Feb 16, 2013 at 12:02 PM
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..? :( 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), "\n\t\t\t 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... :(
Thank you for help!!!
Your code is a bit hard to read because unityawnsers messed up your copy paste. I did not realize you have mutliple scripts and updated my awnser. You should get console errors from either the compiler or when playing the game that should indicate where the problem is :) If its still giving trouble can you attach the scripts?
Sorry, I was a bit out of the home... It is very nice of you to get involved in my trouble. Thank you! So, for now there are two scripts: timer (including guy program$$anonymous$$g) and death script. The case is that there is no error/indication on anything.. The game starts without any problem, but when the timer comes to 0, I trigger GUI Box "game over", there is this naughty button "reset" which now doesn't do anything anymore, since I edited the script. Before it was useless as well, it DID restart the level, but did not reset the timer! So, confusion for me as for not skillful programmer.. First I struggled to create a countdown timer and now with the resetting it.. :) Oke here I'm gonna upload those two... Actually, I don't really think that "death.js" is usefull, but anyways I will include it.. Shit it says the attachment is not permitted, the filetype is wrong... Im gonna try to use a *docx file... I hope u can open it in word or any text editor if u use mac...zipWithScript
The static vars were causing the new loaded scene to show the death menu. Updated the scripts a bit and added comments on the changes. link text
wow! its really advanced script now! I tried already and it works! But, the funny part is that I somehow managed to implement the script you gave me earlier! Just with Awake function and keeping all other stuff, with a few adjustments, like remove the timescale from everywhere, maybe something else, don't really remember. I do just one level with basic interaction like pickUps with simple animations/main$$anonymous$$enu in game Gui, and it is my 3rd work only, I still have a lot of mess in my (around) 30 scripts, which probably could be combined and swapped with space-cunsu$$anonymous$$g once, so a bit hard for now and I still don't get how many useful unity-options work. But the way you commented everything is very valuable for me coz I will use it as a reference!
Thanks a lot! Respect! :)
Your answer
Follow this Question
Related Questions
Help - I need a simple reset/teleport function for my ArchViz project! 1 Answer
Starting/Triggering events based on hover time 0 Answers
Start timer on button press 1 Answer
reset timer using a GUI button 2 Answers
Timed particle system disable 0 Answers