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 Nonnicus · Jun 08, 2014 at 04:12 PM · timerreset

How do i reset a timer once it hits 0?

Im trying to make a timer in my game. What i want it to do is when i click on the icon, I want it to count down. But when it hits 0, i want the timer to reset to the original number and wait for the next click on the timer. I am a huge noob when it comes to coding so any help?

 var timer : float = 5;
 var isClicked = false;
 
 function OnMouseUp(){
     if(timer > 0){
         isClicked = true;
     }
 }
 
 function Update(){
     if (isClicked == true){
         if (timer > 0){
             timer -= Time.deltaTime;
         }
         if (timer == 0){
             timer = 5;
         }
     }
 }
 
 function OnGUI () {
     GUI.Box (new Rect (300, 60, 50, 20), "" + timer.ToString("0"));
 }
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
2

Answer by Jeff-Kesselman · Jun 08, 2014 at 04:13 PM

First of all you have a logic problem here:

 if (timer > 0){
          timer -= Time.deltaTime;
        }
        if (timer == 0){
          timer = 5;
        }
 

What happens if Time.deltaTime is greater then what is left in timer?

Think about it...

Comment
Add comment · Show 1 · 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 Nonnicus · Jun 08, 2014 at 04:22 PM 0
Share

Ok i did fix that after thinking about it. But now that i have that down i just want it to reset to the original number (which it does)and stop (which is doesnt) and wait to be clicked again. Any other way to do that?

avatar image
1

Answer by Nonnicus · Jun 08, 2014 at 07:42 PM

Thank you for your answer! i finally figured a script out that works from that small advise that you helped me with. Heres the new script

 var timer : float;
 
 var isClicked = false;
     
 var sound : AudioClip;
 
 function OnMouseUp(){
     isClicked = true;
 }
 
 function Update(){
     if (isClicked == true){
         if (timer > 0){
             timer -= Time.deltaTime;
         }
         else if(timer <= 0){
             timerReset();
             audio.clip = sound;
             audio.Play();
         }
     }
 }
 
 function OnGUI () {
     GUI.Box (new Rect (300, 60, 50, 20), "" + timer.ToString("0"));
 }
 
 function timerReset(){
     isClicked = false;
     if (isClicked == false){
         timer = 5;
     }
 }
Comment
Add comment · 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

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Time limit on my game with selection menu 0 Answers

Reset the countdown timer 2 Answers

How To Reset Timer on a Collision (C#) 0 Answers

Timer reset after elapsed time. 1 Answer

Resetting Combos 1 Answer


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