Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Baalhug · Dec 05, 2013 at 01:45 AM · coroutinedebugtime.deltatime

Weird coroutine behaviour, speed increases too much

Hi guys.

I have 3 objects with the same script attached. This script has a coroutine. For any reason Time.deltaTime is not working the same way in Update function and inside the coroutine. The visible result of this is Time.deltaTime grows very significantly inside the coroutine, and as i'm using Time.deltaTime to move this objects along a path at constant speed, when the first object reaches a certain point in the path where the coroutine takes control the speed of the object running the coroutine is x20 normal speed (the other objects keep moving at normal speed until they reach the same point). The coroutine calculates the speed the same way than Update function:

Position = Position + Time.deltaTime/TotalTimeOfPath;

When i debug the game i see the values of Position grow faster (and constantly) inside the coroutine. Do you know what is happening?

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 chillersanim · Dec 06, 2013 at 09:15 PM 0
Share

Possibilities:
Or you are updating Position multiple times or you are calling the coroutine to often. Debug it and look how often the coroutine is called in a single frame.

Also, are you calling it from Update()?
Time.deltaTime is working for Update() not for FixedUpdate()...
If you are using events, then check them to.

Can't say much more without a somehow more complete code of your's.

Greetings Chillersanim

avatar image Baalhug · Dec 07, 2013 at 12:05 AM 0
Share

Yes, i'm calling the coroutine from update. When i debug it the yellow line indicating the line about to be executed sometimes becomes green and using "step into" repeats it a few times. Dunno what this means. In the line where the coroutine is called:

 StartCoroutine("$$anonymous$$yCoroutine");

sometimes the yellow line repeats too, and in the first line of coroutine:

 IEnumerator $$anonymous$$yCoroutine(){

it happens the same, but then it goes normal, line by line, as usual.

One more thing, when it reaches the yield call:

 yield return new WaitForEndOfFrame();

Then it jumps out of while loop (as expected) but before continuing on the Update function it jumps to the last line of the coroutine but it does NOT execute it (i watch the variable on that line and it does not change). After this it jumps back to the Update function and finishes, but ins$$anonymous$$d of begin again the Update function until it reaches the coroutine call, it goes directly to the coroutine call ignoring all the code before that line.

2 Replies

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

Answer by ThePunisher · Dec 07, 2013 at 12:24 AM

Since you mentioned you are calling your coroutine from update I'm going to assume that it is not checking if the coroutine has already been started so the update loop will start yet another coroutine on future updates. Eventually you get many coroutines updating the object's position and it gives you that effect you speak of (where the speed increases dramatically). Wrap your StartCoroutine call with an If condition that makes sure it does not get called while the same coroutine is already running.

You can do this using a flag like such:

 private bool m_coroutineStartedFlag = false;
 
     private void Update()
     {
         if (!m_coroutineStartedFlag)
         {
             StartCoroutine(MyCoroutine());
         }
     }
 
     private IEnumerator MyCoroutine()
     {
         m_coroutineStartedFlag = true;
 
         //do code and yielding until done.
 
         m_coroutineStartedFlag = false;
     }
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 Baalhug · Dec 07, 2013 at 12:29 AM 0
Share

Wow, i haven't test it yet but i'm pretty sure you're right. Gonna try it and feedback to you

avatar image Baalhug · Dec 07, 2013 at 12:36 AM 1
Share

Yes, you were right. I'm calling the coroutine once per frame. I think the flag solution is the only in this scenario, i'm actually thinking that maybe this is not the best use for a coroutine. As i have written all the code in update function now i must decide if delete update function and use only coroutines or use the flag solution massively (i began using coroutines to avoid flags). Thank you very much.

avatar image RyanZimmerman87 · Dec 07, 2013 at 12:44 AM 0
Share

Why in the world would you want to use a coroutine every frame, isn't that's what Update is for? If it's really a 2 step process you could use Update and LateUpdate. Or you could just call a new function from Update which is not a Coroutine.

But yes, Coroutines if implemented incorrectly can multiply out of control, I've seen quite a few questions here in the past involving such problems.

avatar image Baalhug · Dec 07, 2013 at 12:53 AM 1
Share

The reason, Ryan, is this: http://answers.unity3d.com/questions/588015/how-to-change-this-using-a-coroutine.html but now i'm changing my code, i will delete Update function at all and use only coroutines and OnGUI (i will have to use nested coroutines, i hope it will not happen the same).

avatar image
0

Answer by ParagonSe7en · Mar 01, 2017 at 07:24 AM

This thread is super old but I found that I had a similar issue that was being caused by a debug statement that I had in a while loop that was in a coroutine I was running. Hopefully this helps some poor lost soul.

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

20 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

Related Questions

Why isn't this coroutine fully firing? 1 Answer

Dash speed doesn't always APPEAR to increase when dash is in progress 0 Answers

Why is my Mathf.Movetowards Coroutine executing instantly? 2 Answers

How to debug a coroutine? 2 Answers

My code is working wierd! Please help me understand why!,My code is working weird! Pleas help me understand why 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