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
4
Question by RoR · Oct 28, 2010 at 06:57 PM · ienumeratorstartcoroutine

IEnumerator Function

IEnumerator C#

What is this? Why is it used? Why are these used with StartCoRoutine?

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

Answer by RampantStudios · Oct 28, 2010 at 07:29 PM

Your best bet is the check the documentation as to why they use IEnumerators within Unity. http://unity3d.com/support/documentation/ScriptReference/WaitForSeconds.html http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

Taken from the StartCoroutine documentation -

The execution of a coroutine can be paused at any point using the yield statement. The yield return value specifies when the coroutine is resumed. Coroutines are excellent when modelling behaviour over several frames. Coroutines have virtually no performance overhead. StartCoroutine function always returns immediately, however you can yield the result. This will wait until the coroutine has finished execution.

The IEnumerator allows the program to yield things like the WaitForSeconds function, which lets you tell the script to wait without hogging the CPU

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
avatar image
3

Answer by boymeetsrobot · Oct 28, 2010 at 07:33 PM

StartCoRoutine essentially runs a function in another thread when used in conjunction with yield. We use IEnumerator functionName() as we are essentially spawning a collection of these functions that are iterated through and executed when used with WaitForSeconds. You can think of it as processing functions with a delay, outside of the Update loop.

Take a look at my answer on the post below for an example:- http://answers.unity3d.com/questions/25246/programming-question-with-waitforseconds/25249#25249

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 Mike 3 · Nov 15, 2010 at 06:28 PM 1
Share

Just for clarity - coroutines are not run in another thread, they're essentially just functions which get the current state saved and added to a queue, then resumed when the yield condition is met

avatar image boymeetsrobot · Nov 15, 2010 at 06:47 PM 0
Share

Thanks for the explanation $$anonymous$$ike. This was based on assumption because WaitForSeconds seemed very similar to Thread.Sleep :)

avatar image hacky97 · Jun 29, 2014 at 11:12 PM 0
Share

t is too bad that unity can still not start new threads. I guess that makes a big deal to the whole unity layer. Luckily some internal functions like LoadlevelAsync() will load the level in a new thread...

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

No one has followed this question yet.

Related Questions

how do i make my spawn system spawn every 2 seconds then wait 10minutes? 3 Answers

How to appear object on trigger delay? 2 Answers

How to make a 3d text writeOut method? 1 Answer

Coroutines IEnumerator not working as expected 2 Answers

StartCorutine not Working 3 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