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
0
Question by Romano · May 09, 2014 at 04:49 AM · javascriptcoroutine

Can you say that a coroutine is just a function with yield somewhere in it?

Would it be true to say "A coroutine is just a function with yield somewhere in it?"

At least in javascript, I realise that in C# you have to use IEnumerator. But am I missing anything else? I'm trying to understand the buggers.

Thanks alot :)

Romano

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 Eric5h5 · May 09, 2014 at 05:05 PM 1
Share

You also have to use IEnumerator for Unityscript. Even if you don't explicitly specify it, it's there, and a coroutine can never return anything other than IEnumerator.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by fafase · May 09, 2014 at 05:03 AM

Not only but somehow yes.

 void Start(){
    Method();
 }

in the case above, when the program hits Method, it stops and jumps to the memory location where Method is stored, then it runs all the code and once it hits the return of the method it gets back where it left off (even when you do not write return, the compiler puts one).

 void Start(){
     StartCoroutine(Coroutine());
 } 

in this situation, same thing happens but then the coroutine is placed on a list of coroutine to be run after the update. What you get is that Start starts (...) then the coroutine starts and hits the yield (you need at least one in the method) then returns to the Start which closes. Next frame after the Update is run, the compiler checks for any pending coroutine and finds that one, then it gets back to the yield it quit from previous frame and continues the coroutine until finding another return (whether yield or basic return).

So in a way, a coroutine is a method but that you can pause.

Coroutine are not thread, more likely a process since if you kill the parent object that triggered the coroutine, the coroutine dies with it.

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 Romano · May 09, 2014 at 05:13 AM 0
Share

Great answer! I don't really feel qualified to say whether it's right or not, but I'll tick it anyway unless anyone wishes to argue with you :) Thanks mate!

avatar image fafase · May 09, 2014 at 05:22 AM 0
Share

There :http://unitygems.com/coroutines/ and you will feel more qualified.

avatar image Romano · May 09, 2014 at 07:56 AM 0
Share

I read that already, but I liked your answer better :)

avatar image
0

Answer by Owen-Reynolds · May 09, 2014 at 04:35 PM

IMHO the main distinction that trips people up is that that coroutines live for a few frames, and how you have to think about that.

A regular function acts and feels like it's part of you. You call it, it runs and completely finishes, and you continue from the next line. They act as if they were cut&pasted into that part of your code.

A coroutine acts like a different person. You run a function; but it feels like you "spawn off" a coroutine. StartCoroutine is more like Instantiating an empty gameObject with a new script on it. It's out there, it will start when it feels like it, do it's own thing, and quit when it feels like it. It won't especially check in with you or tell you when it's done. Sort of a "fire&forget."

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

22 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

Related Questions

changing a coroutine's argument at the coroutine's runtime? 1 Answer

Stop coroutine not working 1 Answer

How do I delay the appearance of these buttons? 3 Answers

Coroutine gets skipped in javascript/unityscript 0 Answers

Keeping track of time? 4 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