Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
15
Question by animeman0 · Jul 11, 2015 at 11:27 AM · c#aiprogrammingbasic

What are IEnumerator and Coroutine?

Just a few quick question?

What are IEnumerator and Coroutine?

What do they do?

How should they be used?

And where is the best place for them to be used?

Example code would be highly appreciated.

I tried looking online but I couldn't find anything that gives me a basic and simple explanation.

Many Thanks in advance!!

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

Answer by fafase · Jul 11, 2015 at 11:45 AM

They are both types representing an object.

Coroutine is a Unity engine class while IEnumerator belongs to the .NET.

When you iterate through a collection or access a large file, waiting for the whole action would stop all others, IEnumerator allows to stop the process at a specific moment, return that part of object (or nothing) and gets back to that point whenever you need it.This until the MoveNext method from IEnumerator returns false indicating we reached the end of the collection/file.

Obviously, it is up to the programmer to implement IEnumerator so that your collection is cut properly, there is no magic. If you make a class a IEnumerator, there will be a bunch of methods to implement so the iteration goes accordingly. For most built-in classes it is already done so you can use it the process without doing anything. For instance:

  foreach (var item in collection){}

This can only work if the collection is a type inheriting from IEnumerator. All arrays, lists and so on do that.

The coroutine is the object within unity that allows to start parallel action (or almost). When using StartCoroutine, unity creates a new object of type Coroutine, this object performs some action and then returns a IEnumerator object (or nothing):

 IEnumerator MyCoroutine(){
     while(true){yield return null;}
 }

It returns a null object or a IEnumerator. A coroutine needs a MonoBehaviour to be attached to so it will use it as a parent process (if familiar with Unix). That parent process checks on each frame if it has a coroutine pending, it does at various time in the frame since you can control when you want the coroutine to get back on:

 IEnumerator MyCoroutine(){
     int index = 0;
     while(true){
         yield return new WaitForEndOfFrame(); 
         Debug.Log("Frame is over");
         if(++index == 3){yield break;}
     }
 }

This one above, will start from code, then run until it hits the return line. At that point, we are in the Update of a MonoBehaviour (or Late/Fixed/Start/...), and it gets back where it stops. At the end of the frame, Unity will look into that MonoBehaviour and find that coroutine, returns from the yield return line and continues. It will do that 3 times until break. On each round it retuns a IEnumerator object from the WaitForEndOFrame method.

Conclusion: IEnumerator is a .NET type that is used to fragment large collection or files, or simply to pause an iteration. Coroutine is a Unity type that is used to create parallel actions returning a IEnumerator to do so.

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 animeman0 · Jul 11, 2015 at 01:47 PM 0
Share

Thank you so much!! I have a better understanding of what they're used for. So again thanks

avatar image
14

Answer by Owen-Reynolds · Jul 11, 2015 at 05:23 PM

Coroutines are ways to write code that says "wait at this line for a little."

To be more precise, WaitForSeconds and "wait until the next frame" (yield return null) are the commands that make code wait at one spot. They can only be used inside Coroutines.

See the docs for examples: http://docs.unity3d.com/400/Documentation/ScriptReference/index.Coroutines_26_Yield.html

A coroutine is a function written with the word IEnumerator in front, just because. You can ignore what IEnumerator is -- it just has to go there. It's really a different C# trick, which is being McGuyvered by Unity, like using a paperclip to pick a lock.

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Making an FPS arcade game. 0 Answers

Making an FPS arcade game. 0 Answers

Distribute terrain in zones 3 Answers

Basic AI Follow Player 2D C# 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