• 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
Question by GutoThomas · Jun 29, 2012 at 01:35 AM · coroutineienumeratortime.timescale

Does coroutines are affected by Time.timeScale?

When I create an IEnumerator like this one:

 IEnumerator Example () {
 
 yield return new WaitForSeconds(20);
 //do something
 
 }

Let's say that in another script I set Time.timeScale = 0 for some time. When I do this, the coroutine continue running to complete the 20 seconds or it pauses while timeScale is equals to 0?

Comment
aldonaletto
flamy
BigRoy
aliakbo
RakshithAnand
yashpal
Pnvanol
Ash-Blue

People who like this

8 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 Blind56 · Aug 22, 2016 at 02:46 PM 0
Share

dat question structure tho

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by aldonaletto · Jun 29, 2012 at 02:08 AM

Setting timeScale to 0 doesn't stop coroutines, but stops WaitForSeconds. Coroutines continue executing each frame, but WaitForSeconds apparently uses the current Time.deltaTime, which becomes zero when timeScale = 0.
The simple test below shows this:

 function TestCoroutine(){
   for (var n=0; n < 5; n++){ // count 5 frames...
     print("frame "+n); 
     yield;
   }
   print("start delay 5 s");
   yield WaitForSeconds(5); 
   print("delay ended");
 }
 
 var pause = false;
 
 function Update () {
   if (Input.GetKeyDown("escape")){
     pause = !pause;
     if (pause){
       Time.timeScale = 0;
       TestCoroutine();
     } else {
       Time.timeScale = 1;
     }
   }
 }

When Escape is pressed, timeScale is set to 0 and TestCoroutine is started - the 5 frames are counted and WaitForSeconds(5) is started, but the 5 seconds wait time lasts forever until you press Escape again to return timeScale to 1.

Comment
flamy
GutoThomas
Rafes
paulygons
BigRoy
aliakbo
RakshithAnand
MarceloPelli
yashpal
ThePersister
Ash-Blue
daspicko
Eldoir
EvilWarren
Schagg
And 3 more...

People who like this

18 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 GutoThomas · Jun 29, 2012 at 03:04 AM 0
Share

That's really handy for my case! Thank you! :)

avatar image IzzySoft · Jun 18, 2015 at 02:16 AM 2
Share

Did you try something akin to this:

     public static IEnumerator WaitForRealSeconds( float delay )
     {
         float start = Time.realtimeSinceStartup;
         while( Time.realtimeSinceStartup < start + delay && cancelShowngTooltip == false )
         {
             yield return null;
         }
     }
     
     private IEnumerator ShowTooltipAfterDelay( float delay )
     {
         yield return StartCoroutine( WaitForRealSeconds( delay ) );
         
         if( cancelShowngTooltip == false )
             Debug.Log( "Show Tooltip now." );
     }
avatar image

Answer by $$anonymous$$ · Jun 11, 2018 at 03:17 PM

Yes, it affects and it's effect is normal when you keep that game objects which contains coroutine ON, While it is on every thing works fine. If you TURN OFF the game object then every thing going to be mess.

Comment
shankarcool
Westland

People who like this

2 Show 0 · 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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutine crashes game 2 Answers

Failed to inplement smooth increment of weapon trail between frames in Mecanim animation 0 Answers

Can't call of type IEnumerable with delegates? 1 Answer

How to send a signal when a runtime navmesh is created? 0 Answers

How to make loop with call to IEnumerator actually pause? 2 Answers


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