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 /
  • Help Room /
avatar image
0
Question by GTHell · May 15, 2016 at 05:50 PM · c#script.coroutineprogrammingienumerator

What is Coroutine, Yiel and IENumerator?

I try but it seem like I can't really understand the concept at all.

Can you explain it in the easy way please?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JedBeryll · May 15, 2016 at 06:28 PM

A coroutine is a method that you can execute through several frames opposite to a regular method which must execute completely within 1 frame. If you want to start a coroutine you create the method as an IEnumerator and you can't just call it like a regular function, it needs to be started with StartCoroutine(FunctionName(parameters)); example:

 void Start() {
     StartCoroutine(Wait(3f));
 }
 
 IEnumerator Wait(float sec) {
     yield return new WaitForSeconds(sec);
     print("complete");
 }

This would wait 3 seconds then print "complete". Only a monobehaviour can start a coroutine, and it must contain a yield statement. Check the unity manual though it gives you a much better explanation than i do. http://docs.unity3d.com/Manual/Coroutines.html

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 fafase · May 15, 2016 at 06:59 PM 1
Share

This is right but a bit wrong, a coroutine does not differentiate from a regular method because it runs over several frames.

 IEnumerator WrongExampleOfCoroutine(){
     if(true) yield break;  // Done in the same frame
    // $$anonymous$$ore code that will not happen
 }

A coroutine is a method that can run over several frames and be completed or stopped when done with it. It allows to run code that should be temporary (tho it could replace the Update method if using a while(true) loop). It also offers extra control as to when in the frame it will be continued.

http://docs.unity3d.com/$$anonymous$$anual/ExecutionOrder.html

avatar image GTHell · May 16, 2016 at 02:56 PM 0
Share

Can we simply use a normal function without a coroutine?

avatar image meat5000 ♦ GTHell · May 16, 2016 at 03:19 PM 1
Share

If you program in some mega intensive task.

 for(everAndEver)
 {
     for(everAndEver)
     {
         Do LOTS AND LOTS
     }
 } //Dont try this 'code'

And try to jam it in to Update or a normal function it will clog up the cpu for a while.

Putting in yield statements allows you to tell it to stop what its doing at some point in that code and come back to it next frame. It spaces out the processing to stop it hogging everything.

Using yield statements can only be done in a coroutine.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Why cant I access the same function twice? 0 Answers

How can i change the value of a variable from an IEnumerator 0 Answers

"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 2 Answers

Need help with c# 1 Answer

[C#] Coroutine just won't stop! 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