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
1
Question by animeman0 · Sep 04, 2015 at 07:34 PM · c#coroutineienumerator

Why do you use Coroutines and how do you use them?

Hello, I'm starting to learn how to use Coroutines but I'm confused. Firstly I don't understand how they make your game more "streamlined". Also I'm not sure how I should use if someone could give a me basic example that would be great. Also what is this for at the end:

 yield return null

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
3

Answer by Pharan · Sep 04, 2015 at 08:24 PM

Consider the situation where you want a script to do something, wait for a while, and then do something else, wait for a few more seconds, then do something else 5 times with 1 second between them.

With coroutines, all you have to do is write:

      void Start () {
           StartCoroutine(SomethingRoutine());
      }
         
      IEnumerator SomethingRoutine () {
           DoFirstThing();
           yield return new WaitForSeconds(1f);

           DoSecondThing();
           yield return new WaitForSeconds(2f);

           for (int i = 0; i < 5; i++) {
                DoThirdThing();
                yield return new WaitForSeconds(1f);
           }
              
      }

Without coroutines, you would have to add and manage timers and object states to know which step of the way it is, and fill your Update method with lots of ifs or switches. Just imagine all the things you'd need to do.

PS

yield return null just means wait for the next update before continuing.

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 · Sep 04, 2015 at 08:29 PM 0
Share

What about when you're just doing regular coding like a player movement script would it make it faster or would it be better to put it in a method and then called the method in update, Like this:

     void Update()
     {
         StayInArea ();
     }
     void StayInArea()
     {
         if(move.position.x >= 7.82f)
         {
             move.position = new Vector2( 7.82f, transform.position.y);
         }
         if(move.position.x <= -7.82f)
         {
             move.position = new Vector2(-7.82f, transform.position.y);
         }
         if(move.position.y >= 4.82f)
         {
             move.position = new Vector2(move.position.x, 4.82f); 
         }
         if(move.position.y <= -4.82f)
         {
             move.position = new Vector2(move.position.x, -4.82f); 
         }
     }
 }

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

29 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

Related Questions

Powerup issue - Speed 1 Answer

IEnumerator's did not read "bool" after yield return new WaitForSeconds. 1 Answer

Why isn't this IEnumerator getting called? 1 Answer

Multiple Cars not working 1 Answer

Make a laser flickering. 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