Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 awesome98boy · Aug 10, 2015 at 06:52 PM · c#scripting problemcoroutinetimer

Unexpected coroutine behavior for simple timer

So I'm trying to implement power-ups in my game that last for a certain period of time.
One of them is a slow-mo, which I want to last for 20 seconds, not including time while paused.
I had a coroutine working yesterday but all of a sudden it's not working. Here's the code for the coroutine itself:

 IEnumerator slow_mo_powerup(){
         Debug.Log ("start");   //coroutine
         Time.timeScale = slowMoScale;     //currently 0.5f
         float start = Time.realtimeSinceStartup;     //this is timeScale independent
         float timer = start;
         while (timer < start + 20.0f) {        //count 20 seconds
             if (Time.timeScale != 0){       //don't count if paused
                 timer = Time.realtimeSinceStartup;
             }
             Debug.Log (timer - start);
             yield return 0;
         }
 
         Time.timeScale = 1.0f;
         Debug.Log ("end");
     }


I call StartCoroutine in a different part of the script. Now, I have tried running this many times and each time, it starts and slows it down, logs "start". But then it only goes through the loop once, logs a very small decimal for (timer - start), and then somehow exits without going through the loop again OR completing the last two lines. It never logs "end" nor speeds back up.

Any ideas for why this isn't working??

Thanks

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
0

Answer by zach-r-d · Aug 10, 2015 at 07:16 PM

Try using "yield return null" instead of "yield return 0" to wait for the next frame. That's a UnityScript-ism that doesn't translate to C#.

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 awesome98boy · Aug 11, 2015 at 04:57 PM 0
Share

I just tried this and it didn't change the behavior.
It logged "start" and then went through the loop once, logging "6.818771E-05", and then exited out of the coroutine completely without logging "end".

avatar image
0

Answer by awesome98boy · Aug 11, 2015 at 06:44 PM

I think I have figured it out, in part thanks to zach.r.d
The script with the coroutine object on it is attached to the power object itself, so when you use your last powerup (as I was during testing), I scripted it to Destroy(gameObject). This, however, stopped the coroutine.
So I fixed it by waiting to destroy the powerup until the timer finishes.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a fill take exactly n seconds to complete 2 Answers

C# | How to delay a method with parameters 2 Answers

Interrupting an Enemy Attack [SOLVED] 1 Answer

Create a countdown in C# (inside Coroutine) 2 Answers

If condition is not true, stop timer and reset. 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