• 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 kathode · Jan 17, 2014 at 01:50 AM · coroutinetimer

Coroutine timer running crazy fast

Hi, I am trying to do a simple timer that counts down from 300 through a coroutine. The problem is, it's over in the snap of a finger. There's no way it's 300 seconds, more like 3. Any idea why? Here's the code.

 public float timeRemaining = 300f;
 
 void Start () {
     StartCoroutine(Countdown());
 }
     
 IEnumerator Countdown()
     {
         while (true)
         {
             for (float timer = 0; timer < 300; timer += Time.deltaTime)
             {
                 timeRemaining -= timer;
                 yield return 0;
             }
         }
 
     }
Comment
Add comment · Show 2
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 getyour411 · Jan 17, 2014 at 01:54 AM 1
Share

I think your 'timer' value is growing exponentially with this struct

avatar image kathode · Jan 17, 2014 at 02:02 AM 0
Share

I get it now, thanks a lot! I don't completely understand coroutines just yet, and I was convinced it was something in how I was calling it. I didn't even think to check through the basics of the for loop. Doh!

It's totally convoluted - I'll rearrange to use the variable directly. Thanks.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by rutter · Jan 17, 2014 at 02:03 AM

That for loop is confusing, and probably not what you want.

You're subtracting timer from your time remaining, but the value of timer increases during each frame. A conventional timer reduces the counter by each frame's deltaTime; your timer reduces the counter by the sum of all deltaTimes.

Try something closer to this:

 void Start() {
     StartCoroutine(Countdown(300f)); //maybe use a smaller delay for testing
 }

 IEnumerator Countdown(float secondsLeft) {
     while (secondsLeft > 0f) {
         secondsLeft -= Time.deltaTime;
         yield return null;
     }
     
     Debug.Log("Countdown finished!");
 }
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
avatar image
1

Answer by robertbu · Jan 17, 2014 at 01:58 AM

I spot a problem with 'timeRemaining'. You should be subtracting Time.deltaTime from timeRemaining, not 'timer'. Timer will continue to grow, so you will be subtracting ever increasing amounts from timeRemaining for each frame.

 timeRemaining -= Time.deltaTime;

BTW: It seems a bit convoluted to structure things this way. Why not just base your for() loop on timeRemaining, bailing out when it is

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

How to set timer for WWW helper? 1 Answer

Problems with Color.Lerp in a Coroutine 3 Answers

How to make WaitForSeconds work as a repeating/looping timer? 1 Answer

Making a fill take exactly n seconds to complete 2 Answers

Active Battle System implementation question. 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