Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 megabrobro · Dec 15, 2017 at 04:04 AM · unity 5coroutinecoroutinesstartcoroutine

Why doesnt my coroutine ever end?

Hi there. I wanted to use a Coroutine to make my game pause for a couple seconds whilst the players state changes (eg. Small or big player like in Super Mario).

Everything looks fine to me after studying the docs, but the code after the coroutine which I wanted to use to unpause the game never gets called and the game stays paused:

     void SwitchPowerStates(PowerState newState)
     {
         currentPowerState = newState;
 
         switch (currentPowerState)
         {
             case PowerState.SMALL:
                 {
                     // code that changes the size
                     break;
                 }
             case PowerState.BIG:
                 {
                     // code that changes the size
                     break;
                 }
         }
 
         StartCoroutine(PauseAndWaitFiveSecs(2f));
         Debug.Log("this gets called before the coroutine is started");
     }
 
 
     void PauseGame()
     {
         Time.timeScale = 0f;
     }
 
     void UnPauseGame()
     {
         Time.timeScale = 1f;
     }
 
 
     IEnumerator PauseAndWaitFiveSecs(float waitTime)
     {
         PauseGame();
         Debug.Log("before time = " + Time.time);
         yield return new WaitForSeconds(waitTime);
         // WHY IS NONE OF THE FOLLOWING CODE CALLED???
         Debug.Log("after time = " + Time.time); // doesnt happen?
         UnPauseGame(); // doesnt happen?
     }


I commented the area of code where I expected to be able to unpause the game. I think i included all the relevant code.

Really hope someone can help me.

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
3
Best Answer

Answer by Bunny83 · Dec 15, 2017 at 04:33 AM

Setting the time scale to 0 affects all game time dependent things which includes Time.deltaTime, Time.time and WaitForSeconds as well. You can use "WaitForSecondsRealtime" instead.

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 megabrobro · Dec 15, 2017 at 11:53 AM 0
Share

@Bunny83 Thank you very much

avatar image
3

Answer by JVLVince · Dec 15, 2017 at 04:31 AM

Well I believe you read the document carefully but maybe you not mentioned this point.

The actual time suspended is equal to the given time multiplied by Time.timeScale. See WaitForSecondsRealtime if you wish to wait using unscaled time.


The WaitForSeconds yield is base on Time.time to count (If I not wrong) so when you set Time.timeScale to 0 in your pause function, then the Time.time will just stop at the moment, the time counter inside the WaitForSeconds will never reach waitTime. So instead of using WaitForSeconds, you should use WaitForSecondsRealtime.

One more thing I think I have to mentioned you is that if you pause by set Time.TimeScale to 0, all object of your games will stop FixedUpdate, so you have to be careful with this approach. Since I don't know what you decide to implement so I just can advice this.

Hope this help. Cheers!!

Comment
Add comment · Show 9 · 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 Bunny83 · Dec 15, 2017 at 04:34 AM 0
Share

Ahh, too slow ^^. +1


ps: setting the timeScale to 0 will still "update" the gameobjects. So they still get their Update methods called. Just FixedUpdate and the physics system is completely halted (mainly because the calling of FixedUpdate depends on the game time).

avatar image JVLVince Bunny83 · Dec 15, 2017 at 04:39 AM 0
Share

Cheers ^^.

avatar image megabrobro JVLVince · Dec 15, 2017 at 11:55 AM 0
Share

Thank you both lots. I clearly didnt read the docs properly enoudh. Hopefully this can $$anonymous$$ch me a lesson coz I should have easily seen this. Cheers very much though

avatar image JVLVince Bunny83 · Dec 15, 2017 at 12:06 PM 0
Share

Tks @Bunny83 for correct me. :) I will edit my answer

avatar image megabrobro JVLVince · Dec 15, 2017 at 12:07 PM 0
Share

Hey Vince, I accept his answer as it appears he was first, but i rewarded you some of my Rep as I felt bad and appreciate both your help :]

Show more comments
avatar image megabrobro · Dec 15, 2017 at 12:11 PM 0
Share

Im not really sure how to implement this. I saw it as instructions somewhere , but they clearly arent that accurate as I dont think it said PauseForRealTime in the instructions. I probably just make a bool flag isPaused and set it true when I start the routine and false when it ends , then I can pause all the update loops etc in my game (pain in ass, when I was coding pure code in Libgdx or LWJGL I could access the very "top-most" update loop (reallly wanted to remember the correct word but that will do lol) and stick a small bool in there and it would suspend all updating for the game loop

avatar image JVLVince megabrobro · Dec 21, 2017 at 03:24 AM 1
Share

I think you should take a look at this. It's have various way to pause a game, including your approaching :D . Cheers

avatar image megabrobro JVLVince · Dec 21, 2017 at 03:53 AM 0
Share

Thanks I will do

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

149 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 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 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 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 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 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

Coroutines IEnumerator not working as expected 2 Answers

understanding coroutines 0 Answers

How to force Coroutine to finish 1 Answer

Startcoroutine not working second time 2 Answers

Coroutines and states 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