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
5
Question by united4life · Dec 02, 2012 at 04:12 PM · basics

StartCoroutine?

Can anyone explain me about Coroutine. I am not able to understand in docs.unity3d.com.

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 fafase · Dec 02, 2012 at 06:23 PM 0
Share

There is an article on coroutines here: http://unitygems.com/coroutines/ if you want to read about it.

2 Replies

· Add your reply
  • Sort: 
avatar image
14

Answer by aldonaletto · Dec 02, 2012 at 05:04 PM

When you start a coroutine, it's executed until an yield instruction is found. If it's an "empty yield" (returns nothing in JS, or returns null in C#), the coroutine is suspended and control returns to the calling code, as if the coroutine has finished - but it's still alive and kicking! Next frame, the coroutine will wake up automatically in the instruction following the yield, and this will be repeated until the coroutine reaches its last instruction, when it dies silently.

 yield; // "empty yield" in JS
 
 yield return null; // "empty yield" in C#
 

When you yield to another coroutine, on the other hand, control is passed to this coroutine, and the calling coroutine gets suspended at that yield until the called coroutine finishes:

 yield AnotherCoroutine(); // yielding to AnotherCoroutine in JS
 
 yield return StartCoroutine(AnotherCoroutine()); // the same in C#

Each coroutine you start allocates an object in the heap, and will be there until its end is reached. Care must be taken when starting a coroutine in periodic functions like Update, LateUpdate, FixedUpdate etc. because you can crowd the memory with thousands of coroutines executing in parallel - the frame rate drops like a rock, and Unity may even crash when enough coroutines are stealing CPU time and memory. Usually a boolean flag is used to avoid such disasters: you start the coroutine only when the flag is false, set it at the coroutine beginning and clear it at the end, like this:

 var executing = false;
 
 function Update(){
   if (executing == false){
     StartCoroutine(ACoroutine());
     print("Coroutine started");
   }
 }
 
 function ACoroutine(): IEnumerator {
   executing = true; // set the flag
   yield WaitForSeconds(10.0);
   print("Coroutine ended");
   executing = false; // clear the flag before returning
 }

This code prints "Coroutine started" at first, and 10 seconds after "Coroutine ended" magically appears in the console, then Update will print "Coroutine started" again, and so on.

If you want an example of how to use a coroutine, take a look at this question, for instance.

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
1

Answer by nventimiglia · Dec 02, 2012 at 06:11 PM

     IEnumerator MyCoroutine()
     {
 
         // wait ONE FRAME and continue
         yield return null;  // SIC - that's null, not "1" or "frame" or anything else
 
         // wait 5 seconds and continue
         yield return new WaitForSeconds(5);
 
        
         // Custom Update Routine which repeats forever
         do
         {
             // wait one frame and continue
             yield return 1;
 
             if (IWantToEndEarly)
             {
                 // end
                 yield break;
             }
 
             
         } while (true);
 
 
         // end naturally
 
     }
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 Eric5h5 · Dec 02, 2012 at 06:21 PM 2
Share

The 1 in "yield return 1" doesn't mean it waits one frame. You should use "yield return null" ins$$anonymous$$d.

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

14 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

Related Questions

Help with Path Finding in my game. 1 Answer

Is there a way to make OnTriggerEnter work only for the colliders marked as triggers? 2 Answers

List of C# scripting definitions 1 Answer

Trouble with switching script 1 Answer

How would i make an inventory like destiny? 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