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 Steven-1 · Nov 10, 2014 at 08:04 PM · coroutinecoroutines

problem with coroutines

I'm not all that familiar with coroutines in Unity. I have this:

     private IEnumerator TurnCoroutine()
     {
         Debug.Log("TurnCoroutine() 1");
         yield return DoTurn();
         Debug.Log("TurnCoroutine() 2");
     }
 
     private IEnumerator DoTurn()
     {
         //TODO: do stuff
         Debug.Log("DoTurn() 1");
         yield return new WaitForSeconds(2); //temp
         Debug.Log("DoTurn() 2");
     }

And I would expect it to log like this:

  • TurnCoroutine() 1

  • DoTurn() 1

  • TurnCoroutine() 2

  • DoTurn() 2

But it only logs this:

  • TurnCoroutine() 1

  • TurnCoroutine() 2

it doesn't seem to run DoTurn(), can anyone explain to me why?

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

Answer by Bunny83 · Nov 10, 2014 at 08:14 PM

Because coroutines have to be started with StartCoroutine ;)

 private IEnumerator TurnCoroutine()
 {
     Debug.Log("TurnCoroutine() 1");
     yield return StarCoroutine(DoTurn());
     Debug.Log("TurnCoroutine() 2");
 }
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 Steven-1 · Nov 10, 2014 at 08:17 PM 0
Share

ah thanks, just found out myself, was already typing my answer before yours appeared

avatar image
1

Answer by Steven-1 · Nov 10, 2014 at 08:16 PM

Why is it that I always can't seem to find the answer until I ask the question, sigh, just found out I have to this:

 yield return StartCoroutine(DoTurn());

instead of

 yield return DoTurn();

which I guess is logic, but if that is desired, why does what I did compile?

Comment
Add comment · Show 3 · 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 · Nov 10, 2014 at 08:31 PM 2
Share

It compiles because your method DoTurn just returns an object and that you returned to the coroutine scheduler of your TurnCoroutine. The scheduler will treat all values that aren't derived from YieldInstruction like "null" and will wait 1 frame.

The object your DoTurn method returns is an instance of a compiler generated (internal) class that implements the IEnumerator interface which actually contains the code of your coroutine. When you pass such an object to StartCoroutine you don't actually "start" or "run" the code inside your coroutine. You just pass this object to Unity and the scheduler will store it internally and "schedule" the next "iteration" of your generator method depending on what you returned in your coroutine.

See the docs on yield and the wiki page on generator

avatar image Steven-1 · Nov 10, 2014 at 10:27 PM 0
Share

thanks for the info

avatar image Kiwasi · Nov 10, 2014 at 10:35 PM 0
Share

Translation: Coroutines are a Unity hack, not a language feature. As such there are cases where the compiler won't detect problems, because your code is still legal in the language.

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

27 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

Related Questions

NullReferenceException in StartCoroutine 1 Answer

Flash image while ammo is low - coroutines? 3 Answers

Coroutines not passing yield 1 Answer

Collection Change During Iteration 0 Answers

Why doesnt my coroutine ever end? 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