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 Sendatsu_Yoshimitsu · Sep 19, 2014 at 09:12 PM · c#coroutinetimer

Making a fill take exactly n seconds to complete

I'm working on a timer class I can call for a whole bunch of different purposes, and as part of it I'm trying to write a function that will gradually draw a sprite with a radial fill such as to indicate the timer's progress- when the sprite is invisible, it isn't running, when it's completely drawn the timer has completed. Currently I'm running the timer in one coroutine, and the fill in another:

 IEnumerator CountDown(float time){
     countdownRunning = true;
     StartCoroutine("CountDownAnimation",time);
     yield return new WaitForSeconds(time);
     Debug.Log ("Ding ding ding");
     if(countdownRunning)
         //do stuff
     
     countdownRunning = false;
 }

 IEnumerator CountDownAnimation(float time){
     float animationTime = time;
     while (animationTime > 0) {
         animationTime -= Time.deltaTime;
         countdownSprite.fillAmount = animationTime/time;

     }
     yield return null;
 }

The obvious problem is that since coroutines don't run once per frame, animationTime continually returns true and the sprite jumps instantly from 0% fill to 100% fill. Is there a very simple way that I'm missing to correctly set the fill each frame, given that the waitforseconds() on CountDown precludes setting it in the same loop that runs the timer?

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 Sendatsu_Yoshimitsu · Sep 20, 2014 at 12:45 AM 0
Share

Oh hey, I'm a fool, that works perfectly- if you want to put this in an answer I'll mark it right, either way thank you! :)

2 Replies

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

Answer by VesuvianPrime · Sep 19, 2014 at 09:27 PM

I think your mistake is putting the yield statement outside of the loop

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 Sendatsu_Yoshimitsu · Sep 20, 2014 at 01:00 AM 0
Share

Thanks again :)

avatar image
-2

Answer by Slev · Sep 19, 2014 at 09:23 PM

Inside of your while loop place a:

 yield return new WaitForSeconds(n);

this will wait n seconds (n being a float that can be less than 1) before the loop resume again. Since you seem to be filling with detlaTime you should be able to use

 yield return new WaitForSeconds(deltaTime)
Comment
Add comment · Show 7 · 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 VesuvianPrime · Sep 19, 2014 at 09:28 PM 0
Share

That would make the loop take twice as long to complete. You don't need to WaitForSeconds to wait for the next frame

avatar image Sendatsu_Yoshimitsu · Sep 19, 2014 at 09:30 PM 0
Share

That fills smoothly and looks great, but it doesn't seem to sync up with the timer, if I feed them both a value of 2f the timer takes two seconds to finish, and the fill takes about 3... is that a problem with how I've structured things?

avatar image Slev · Sep 19, 2014 at 09:30 PM 0
Share

@VesuvianPrime well thanks for re$$anonymous$$ding why I never help anyone on these stack sites.

avatar image VesuvianPrime · Sep 19, 2014 at 09:32 PM 0
Share

No need to take it personal

avatar image Sendatsu_Yoshimitsu · Sep 19, 2014 at 09:36 PM 0
Share

So I'm sorry if it's an obvious question, but without using WaitForSeconds what's the obvious way to hold execution of a coroutine until the next frame?

Show more comments

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

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

Related Questions

Game over when time reached 1 Answer

Start Coroutine after other has finished 4 Answers

Multiple Cars not working 1 Answer

Why does this coroutine freeze my game? 1 Answer

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