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 greatUnityGamer · Sep 09, 2013 at 03:12 PM · timervariables

Counting Down Timer But My Variable is not reaching Zero ever, Why?

ok this is my code and it never seems to reach zero. I don't understand why my Variable myTimer doesn't reach zero ever. I even put a debug messege thing to tell me if myTimer more than zero and also when my timer is zero. but the one to tell me when my timer is zero never appeared cause like i said it doesn't seem like myTimer is reaching zero. The following code is in my Update() method

 As soon as user clicks key G, set myTimer to 4.0f (forseconds)
 //By the way myTimer is a float
 
 
 if( myTImer > 0 )
     myTimer -= Time.deltaTime;
 
     //do something
 }
 else if( myTimer == 0){
 
     //do something until count down something
     //when count that something
     //then set myTimer = -1
 
 }
Comment
Add comment · Show 1
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 AlucardJay · Sep 09, 2013 at 03:54 PM 0
Share

line 5 has a spelling mistake and is missing an opening parenthesis. Always check your spelling and opening/closing brackets/parenthesis.

 if( myTImer > 0 ) // capitol I, no opening curly braces

should be

 if( myTimer > 0 ) {

1 Reply

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

Answer by DannyB · Sep 09, 2013 at 03:15 PM

Use else if( myTimer <= 0 ) (or just else) instead of else if( myTimer == 0 )

There is very little chance that reducing Time.deltaTime from any number would reach 0 exactly.

Also, when dealing with floats, you should consider using Mathf.Approximately for float comparison. It will not help you (and not necessary) in this case, but you may need it for other cases.

Finally, for a proper debug in the future, you should have placed a Debug.Log( myTimer ) outside of both conditions, and you could have seen the values going through zero.

Comment
Add comment · Show 3 · 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 JoaquinRD · Sep 09, 2013 at 03:19 PM 1
Share

I would just change "else if ( myTimer == 0 )" to a simple "else".

avatar image DannyB · Sep 09, 2013 at 03:22 PM 1
Share

Wanted to demonstrate the importance of

avatar image greatUnityGamer · Sep 09, 2013 at 08:27 PM 0
Share

That worked perfect. thanks

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Delay between throwing/firing object 1 Answer

Countdown Timer location 1 Answer

Why are variables assigned through the inspector considered not assigned when prefab is spawned? 1 Answer

Making a fill take exactly n seconds to complete 2 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