- Home /
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:
How do coroutines work?
In what functions can I use them? I tried to put one in Update() but that did not work.
When SHOULD I use them?
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
You've seen the docs for it?:
http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
Answer by Bluk · Aug 06, 2012 at 04:57 PM
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
You can start them pretty much everywhere. In the
Update()
it should work, so if it didn't then you did something wrong.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.
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.
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
}
Thank you, all of you! Seth- thank you very much. That clears a lot up.
Answer by Mander · Aug 06, 2012 at 04:53 PM
here u will find your 1 and 3 answers coroutines
u can use coroutines in any function but Update and FixedUpdate
u can find the anwer here: enum variables
hope this helps
not entirely accurate, any $$anonymous$$onobehavior with fixed calls cannot contain coroutines (such as OnGUI)
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
Follow this Question
Related Questions
iTween Visual Editor Event Call from C# to Javascript 1 Answer
Unityscript: Accessing functions from other scripts 1 Answer
Respawn on Collision Problems. 2 Answers
Interacting with objects 2 Answers
Attach object as child using code 1 Answer