Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by rashiddev · Dec 18, 2018 at 02:14 AM · coroutinemonobehaviourcoroutines

Yield return null?

Please someone explain to me what is "yield return null" in coroutines?

For example if we have this code..

 void update{
    StartCoroutine(MoveEnemy());
 }
 
 IEnumerator MoveEnemy(){
    While(true){
       somecode...
       yield return null; // Whats the use of this line?
    }
 }

I know it means run the code until the next frame, but what does that even mean?

Thank you.

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 pako · Dec 18, 2018 at 08:41 AM 0
Share

@rashiddev since your question has already been well answered, I'll just add as a side note that it's bad practice to start a coroutine inside Update, especially if it's done unconditionally. It always leads to problems. For example, in the code you posted, a new instance of the $$anonymous$$oveEnemy() coroutine will be started in every single frame. So at 60FPS, 600 $$anonymous$$oveEnemy() instances will have been created after 10 seconds of the Update execution, each running the while() loop. Not something that you'd want, or do intentionally.

3 Replies

· Add your reply
  • Sort: 
avatar image
10

Answer by Bunny83 · Dec 18, 2018 at 04:03 AM

The yield keyword is not a Unity feature. It's a C# feature called "iterator block". It allows to easily write iterator objects. The C# compiler actually turns your coroutine method into a compiler generated state machine class.


Unity uses those to implement coroutines. They are actually meant for things like this:

 IEnumerable<int> SomeIterator()
 {
     yield return 0;
     yield return 42;
     yield return 7;
     for (int i = 0; i < 10; i++)
         yield return i*2;
 }

This is also called a "generator method". When you call this method you actually create an instance of the internally generated statemachine class which implements the IEnumerable interface. This can be used for example in a foreach loop:

 foreach(int val in SomeIterator())
 {
     Debug.Log(val);
 }

This will print those 13 values: 0, 42, 7, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18. In Unity's coroutines Unity uses the value you return to determine when to continue / iterate your statemachine. If you return the value null (or any other value Unity doesn't recognise) Unity will simple re-schedule the coroutine the next frame. However if you return an instance of the WaitForSeconds class, Unity will wait for the specified time until it continues your coroutine.


This great blog article (which unfortunately only exists in the web archieve) explains this quite well with a simple example

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 Andrey-Postelzhuk · Dec 18, 2018 at 11:27 AM -1
Share

The yield keyword is not Unity feature. But StartCoroutine() is Unity feature. StartCoroutine() function works with iterator in it's own way.

avatar image
9

Answer by Andrey-Postelzhuk · Dec 18, 2018 at 09:56 AM

Short answer:

yield return null - wait for the next frame and continue execution from this line

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 darkStar27 · Dec 18, 2018 at 10:06 AM -1
Share

It does not wait for end of frame, that is WaiForEndOfFrame, yield return null is updated many time in single frame.

avatar image Hellium darkStar27 · Dec 18, 2018 at 10:45 AM 0
Share

yield return null is updated many time in single frame

That's not what the diagram you provided shows. The control of the coroutine yielding null is returned after each call to all the Update functions, which are called once per frame.

avatar image Andrey-Postelzhuk darkStar27 · Dec 18, 2018 at 11:23 AM 2
Share

I wrote "wait for the NEXT frame". This is not the same as "end of the frame". And "yield return null" definitely called only once per frame. Read your own answer again.

avatar image AceOfHeartsX3 · Aug 15, 2020 at 06:24 AM 0
Share

And in a while(true) loop inside a coroutine, that means the program (game) breaks out of the endless while loop to do everything outside the while loop until the next frame update, then continues the while loop from that line.

So, you need the yield return null in the while(true) loop because without it, a while(true) is an infinite loop that would in theory crash your game. At least that's my understanding.

avatar image
1

Answer by darkStar27 · Dec 18, 2018 at 04:20 AM

Unity performs many operations every frame:

  • Initialization

  • Physics Updates

  • Input processing

  • Game Updates (Update())

  • Rendering Graphics, GUI etc.

yield return null runs earliest of any other yield returns.

For eg: yield return new WaitForEndOfFrame() will work after yield return null.


Explaination:

When you use iterations (IEnumerator) without returns, Unity runs it countless amount of time in one frame, blocking the editor. Therefore, we have to use yield return to return the control back to unity as if it gains control back even for a split second the editor will not block.


You can know more about unity execution order from given flowchart: alt text


unity-execution-order.png (86.7 kB)
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

109 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 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 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 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

Example of co-routine for documentation 1 Answer

Trouble understanding coroutines 3 Answers

Can't get while loop to execute more than once in a coroutine before yielding [Solved] 2 Answers

Coroutine stopping before it is complete 1 Answer

Need opinions, or facts, about how to best go about programming this behavior. Basically Redstone. 0 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