• 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 youngapprentice · Aug 06, 2012 at 04:28 PM · javascriptcoroutine

Coroutine Help

Hello, all! I have been coding for a long while and have had some years of experience with Unity. I am dreadfully confused, however, about coroutines. I understand that they are extremely useful and allow me to do things "On the side". I have read about them but for some odd reason cannot grasp how to use them. For instance, this is my basic Lerping function:

 private var lerp = 0.0;
 private var track = 0.0;
 private var value : float;
 var time : float;

 function lerpPos(){
  if(track < time){
  track += Time.deltaTime;
  lerp = track/time;
  value = Mathf.Lerp(start, end, lerp);
  }
 }

And I would usually call LerpPos() within the Update() function. But, to my understanding, it would be much more useful and more efficient if I made it a for or a while statement in a coroutine.

My questions:

  1. How do coroutines work?

  2. In what functions can I use them? I tried to put one in Update() but that did not work.

  3. When SHOULD I use them?

  4. What is an enum? Yeah it's a bit unrelated but I'd like to know.

Know that I work in Javascript. Thanks, all! I hope you guys can help me out a little, here! - YA

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 Owen-Reynolds · Aug 06, 2012 at 04:51 PM 0
Share

You've seen the docs for it?:

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by Bluk · Aug 06, 2012 at 04:57 PM

  1. I assume that you've already read those: http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html?from=index Coroutines are like threads. They are functions that will be executed "on the side" of all other standard functions (Update, etc...). Not sure if what I say is clear, but when you start a coroutine it will execute in parallel of everything else

    1. You can start them pretty much everywhere. In the Update() it should work, so if it didn't then you did something wrong.

    2. You should use them whenever you need to. For instance you need to delay an action. What you might want to do is start a coroutine that waits for x seconds, then performs such action. That way you don't delay the execution of anything else. You could also use it to periodically do things, like a coroutine that increases your ressources every 1 seconds.

    3. enum stands for enumeration, which is a type you define to be a fixed set of constants. For instance there is an example of use there: http://answers.unity3d.com/questions/15250/how-to-declare-and-use-a-enum-variable-in-javascri.html

Cheers,

Bluk.

Comment
Add comment · Show 2 · 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 Seth-Bergman · Aug 06, 2012 at 05:07 PM 1
Share

To clarify here: you may call a coroutine from Update, but Update may not BE a coroutine..

so you cannot have the line :

yield WaitForSeconds(time);// this is not a coroutine, but a special command which is used in one

inside Update, but you can start the coroutine that contains it from there

StartCoroutine(Wait());

or just

Wait(); // javascript only

function Wait(){ //this is the coroutine

DoSomething();

yield WaitForSeconds(time);

etc

}

avatar image youngapprentice · Aug 06, 2012 at 05:52 PM 0
Share

Thank you, all of you! Seth- thank you very much. That clears a lot up.

avatar image
0

Answer by Mander · Aug 06, 2012 at 04:53 PM

here u will find your 1 and 3 answers coroutines

  1. u can use coroutines in any function but Update and FixedUpdate

  2. u can find the anwer here: enum variables

hope this helps

Comment
Add comment · Show 2 · 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 Seth-Bergman · Aug 06, 2012 at 04:58 PM 0
Share

not entirely accurate, any $$anonymous$$onobehavior with fixed calls cannot contain coroutines (such as OnGUI)

avatar image Mander · Aug 06, 2012 at 05:12 PM 0
Share

didn't know about that. anyways if u try to use a coroutine in a function that cant use them the console.log with tell u in an error.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

How to Implement System.StringBuilder into JavaScript? 2 Answers

Drag Script Not Working 1 Answer

StartCoroutine gets the arglist type wrong 1 Answer

Simple code is not recognizing tag anymore. 0 Answers

Detect OnTriggerEnter with tags 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