Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sebbo1473 · Feb 16, 2013 at 09:59 AM · timerreset

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;

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sebbo1473 · Feb 16, 2013 at 10:16 AM 0
Share

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

avatar image Bluestrike · Feb 16, 2013 at 10:17 AM 0
Share

Updated my awnser with that :)

avatar image
0

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!!!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bluestrike · Feb 16, 2013 at 01:25 PM 0
Share

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?

avatar image sebbo1473 · Feb 16, 2013 at 09:27 PM 0
Share

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

scripts.zip (128.6 kB)
avatar image sebbo1473 · Feb 16, 2013 at 09:27 PM 0
Share

Thank you in front!

avatar image Bluestrike · Feb 17, 2013 at 06:52 PM 0
Share

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

timer2.zip (2.0 kB)
avatar image sebbo1473 · Feb 18, 2013 at 02:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges