Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Priyanshu · May 27, 2015 at 07:14 AM · c#coroutinepauseyield waitforseconds

How to pause a Coroutine?

I am using Coroutines which yield, WaitForSeconds method. I want to implement a Pause system which pauses these coroutines too.

That is if a Coroutine continues every 5 seconds. And if I pause that coroutine at 3 seconds. I need it to continue after 2 seconds after I unpause.

Why do I want this?

  1. I don't want to set timeScale to 0 because I want animations while game is paused.

  2. By using Pause boolean in a if condition, I can pause only those coroutines which run every frame.

  3. I can achieve this functionality by using Update() method. But then there will be no point in using coroutines at all.

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 Priyanshu · May 27, 2015 at 08:39 AM 0
Share

@digzou This is what I wanted. Thanks :)

I guess I asked the wrong question for my Problem.

avatar image digzou · May 27, 2015 at 09:11 AM 0
Share

Converting it to an answer, pls approve it.

5 Replies

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

Answer by digzou · May 27, 2015 at 07:32 AM

If you want to use Animations when timescale is 0, U can use the AnimatorUpdateMode.UnscaledTime property. And use timescale = 0 to pause the game.

http://docs.unity3d.com/ScriptReference/AnimatorUpdateMode.UnscaledTime.html

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
5

Answer by f4bo · May 27, 2015 at 07:40 AM

how about to put this in your coroutine?

     for (int i = 0; i < 5; i++) {
         while (paused) {
             yield return new WaitForSeconds (0.2f);
         }
         yield return new WaitForSeconds (1f);
     }
Comment
Add comment · Show 2 · 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 Priyanshu · May 27, 2015 at 09:15 AM 0
Share

hi @f4bo. Thnx for the answer.

This can be a cool method to pause any particular coroutine (with yield WaitForSeconds), with an Offset of 1 second only.

But it also runs every second ins$$anonymous$$d of 5 seconds. And when paused every .2 seconds

avatar image triangle4studios Priyanshu · Apr 18, 2021 at 01:08 AM 0
Share

.1 - .3 seconds is the industry standard, as any longer will have a visibly noticeable delay before actions continue. This will often make your game appear to be lagging. Keep it low.

avatar image
1

Answer by ShabihDesperado · May 27, 2015 at 07:42 AM

 While(timeCount < 5)
 {
   if(!isPaused)
     timeCount += Time.Deltatime
   yield return null;
 }
Comment
Add comment · Show 4 · 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 Priyanshu · May 27, 2015 at 09:18 AM 1
Share

@ShabihDesperado This can be a solution to pause a coroutine. But it pauses only those coroutines which run every frame.

avatar image triangle4studios Priyanshu · Apr 18, 2021 at 01:09 AM 0
Share

All coroutines run every frame. Unless they are not active, and then they are no longer relevant to this post.

avatar image ShabihDesperado · May 27, 2015 at 09:56 AM 0
Share

But you said that you didn't want to touch timeScale, so the only solution (with your restrictions) is this. The best solution is the one of @digzou but is not what you asked neither.

avatar image Priyanshu · May 27, 2015 at 10:54 AM 1
Share

@ShabihDesperado

As i stated in point no. 2 -

By using Pause boolean in a if condition, I can pause only those coroutines which run every frame.

I wanted something like: When pausing -

  1. Stop the coroutine.

  2. save its state. For example:- if WaitForSeconds(5). And i stop the coroutine at 3 seconds. save state like 2 seconds remaining.

And when unpausing -

  1. Start the coroutine from that saved state. Thats is resume after 2 seconds.

These things are not possible without a timer. But Unity might be using something internally to resume an yielded coroutine.

Well anyways thnx for the answer :)

avatar image
0

Answer by Chambers88 · Jan 24, 2019 at 02:53 PM

Unfortunately there's no easy way to do it...

There seem to be some assets that solve your problem, though I haven't tested them out myself: https://assetstore.unity.com/packages/tools/safe-coroutine-17316

I personally stopped using coroutines for things like enemy behaviors that needed to be paused/resumed in my game :(

Use Update() instead and enable/disable the script. Depending on what you're doing, a state machine might work to simulate the steps of your coroutine inside the update.

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
0

Answer by eomerb · Apr 15, 2020 at 08:29 AM

hello there I know its old topic but while I was searching for an answer I come up this idea what I want to do is display an image for given seconds

  void playImage()
     {
         WWW www = new WWW(url);
         image.texture = www.texture;
         totalWaitTime = duration;
         currentWaitTime = 0;
         StartCoroutine(wait());
     }
     IEnumerator wait()
     {
         if (currentWaitTime < totalWaitTime && !isPaused)
         {
             yield return new WaitForSeconds(1);
             currentWaitTime++;
             StartCoroutine(wait());
         }
         else if (currentWaitTime <= totalWaitTime && isPaused)
         {
 
         }
         else
         {
             //continue
         }
     }

instead of waitforseconds(duration) I wait for 1 second and check if not paused call StartCoroutine(wait()); again and check for the next second, if it is paused do nothing and when continue is pressed or triggered I call StartCoroutine(wait()); again instead of whole second u can divide it to smaller times like 0.1f its up to you I find it pretty easy and hope it will help others

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

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

Reset WaitForSeconds Coroutine 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Coroutines /WaitForSecond ain't working 2 Answers

Is there a way to make WaitForSeconds ignore time.timescale? 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