• 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 samqweqwe · Oct 03, 2014 at 10:04 PM · coroutineyieldwaitforsecondsdelay

Coroutine not running after yield return new WaitForSeconds

I have a coroutine that is used to move objects.

 IEnumerator Move(float start, float end, float length, float delay)
         {
             yield return new WaitForSeconds(delay);
     
                 for (float i = 0; i < 1; i += 0.01667f*(1/length))
                 {
                     MoveEndTapeTex =  Mathf.Lerp(start, end, i);
                     yield return null;
                 }
                 MoveEndTapeTex = end; 
         }

The problem was that the coroutine never passes the yield return new WaitForSeconds(delay); if the delay is greater 0. Does anyone know what the problem is and how to fix it?

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 Eric5h5 · Oct 05, 2014 at 08:06 AM 0
Share

Your loop increment doesn't make any real sense, it's just an arbitrary number and therefore won't work. You need to use Time.deltaTime.

3 Replies

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

Answer by BenKurdziel · Oct 04, 2014 at 05:32 AM

T$$anonymous$$s won't be a very intelligent answer, but if I remember correctly, for loops in IEnumerators are HIGHLY advised against for performance and stability reasons. Also, have you tried putting a Debug.Log("Test"); after the yield to see if anyt$$anonymous$$ng is called afterwards? It's possible the following code just doesn't function like you'd like it to.

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 samqweqwe · Oct 04, 2014 at 11:04 AM 0
Share

i have tested with the debug.log and it does not run if there is a delay. Is it just for loops that are not advised in IEnumerators because i do not know how to do this task without a type of loop eg while

Thanks

avatar image BenKurdziel · Oct 04, 2014 at 09:20 PM 0
Share

Just to be sure, you are using "StartCoroutine(Move(params));" and not just "Move(params);" correct?

And you can totally use any type of loop in a coroutine. Sometimes it's necessary depending on what you are doing. But there's a little trickiness in managing them because you need to Stop them. They can also be somewhat processor intensive depending on how many you are using and how much they are doing each time.

avatar image samqweqwe · Oct 04, 2014 at 10:20 PM 0
Share

I am using startcoroutine Thanks for the info about loops :)

avatar image smallbit · Oct 05, 2014 at 05:56 AM 4
Share

Just a thought: WaitForSeconds will not return when timescale =0.

avatar image Eric5h5 · Oct 05, 2014 at 08:04 AM 1
Share

What? There are no issues at all with loops in coroutines. That doesn't even begin to make sense, sorry.

Show more comments
avatar image
9

Answer by akeplinger · Aug 07, 2015 at 02:27 PM

smallbit has the solution in my case. One developer started using timescale=0 when a game is paused. We were using a coroutine to handle fading up a loading screen and it was never getting past the first yield and t$$anonymous$$s was why. When Time.timescale=0 WaitforFixedUpdates don't happen and waitforseconds never returns. Just setting timescale to 1 at the start of the function fixed the issues.

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 fonko · Nov 06, 2015 at 01:44 PM

in my case it wasn't the timescale or the object being destroyed.. (its my game manager so its live during all the game) but i forgot that i used StopAllCoroutines(); around my code and it was killing t$$anonymous$$s particular coroutine with other routines that actually needed to be killed... so now i need to figure out how to let t$$anonymous$$s particular coroutine alive and kill the others.

check if you have a StopAllCoroutines(); around your code.

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.

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

31 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

Related Questions

What is wrong with this use of WaitForSeconds? 1 Answer

why does Coroutine 'yield return 0' sometimes take a lot of frames before it continues 1 Answer

How to create a delay in a function being called in update? 2 Answers

Alternative to "yield return WaitForSeconds()" in Coroutine with .NET Framework (Unity 2018.4) 0 Answers

Loop Animation with delay-variable 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