Using Couroutines in another Class (Javascript)
Here's the first class
public class Coroutiner extends MonoBehaviour
{
public function Coroutiner()
{
}
public function waiting(num: int)
{
yield WaitForSeconds(num);
}
}
And here's the second class (which contains the first class):
public Class ClassName
{
private var coroutinevar: Coroutiner;
//Other Variables
public function ConstructorName()
{
//Other Variables being initialized
coroutinevar= ClassName.AddComponent(Coroutiner);
Example();
}
public function Example()
{
Debug.Log("NO");
yield coroutinevar.waiting(5);
Debug.Log("YES");
}
//Other functions
}
Basically, when I tried to use the function in a statement checking to see if a keyboard key was pressed, it didn't work at all. If I extend monobehaviour with the second class, it works, but I get a warning, plus I would like to know how to do it outside of a class, especially since I've been trying to figure this out on my own for a while.
If I use the function within a normal script it works fine (the yield objectname.waiting(integer)), but using it within a class just causes that segment to be skipped entirely.
Are you sure you're doing it right? Not sure how it works in UnityScript when there are no IEnumeratorts, but in C# this will not work for sure.
As far as I know, yes. I believe couroutines in java are basically written the same as normal functions.
I THIN$$anonymous$$ whatever is in a function gets executed in one frame no matter what. A coroutine starts a new thread which can take several frames to complete. So if you want to wait for something the coroutine needs to execute the code that should come afterwards. (not sure about this though i don't use coroutines very often).
Actually coroutines do not start new threads. The difference between normal functions and coroutines in Unity is that coroutines' execution can be stopped while normal functions should be completed in one frame.
Your answer
Follow this Question
Related Questions
Collection was modified; enumeration operation may not compute 0 Answers
How do I access a variable/class from a C# file in a JS file? 1 Answer
UnityScript: BCE0048: Type 'ScriptFileName' does not support slicing. 0 Answers
Trying to use coroutines in order to use Mathf.Lerp, Lerp instantly jumps values 0 Answers