Yield and javascript classes

Does anyone know why its not possible to use the yield function in a javascript class?
That is, if I write a function in a class and then instance it in an object if it contains a yield call it will simply exit the function never to return. Take the code out of a class and attach it straight to a game object and everything is cool.

Hi Monark,

Do you use the WaitForSeconds function along with yield? I got yield working just fine in my JS scripts. Maybe if you post some code we can find out what the problem is.

I’ll try and put a simple example together. I’m not using WaitForSeconds I’m using my own functions, so something like this

class foo {
     function bar () {
        //do something
        yield;
     }
}

function Start() {
   var instFoo : foo = new foo();
   yield instFoo.bar();

}

In this case the //do something bit in bar never executes

if I do this

function bar () {
    //do something
    yield;
}

function Start() {
   
   yield bar();
}

then it works ok, and bar runs as expected.

I just typed this straight in so ignore any syntax errors…

Hello,

I have the same exact problem. I found it in several classes I wrote.
Any hint or solution to this problem?

Use StartCoroutine; in this particular case it won’t work if you leave it out.

–Eric

Hello,

I just tried it, but it seems there is no way to work in javascript. StartCoroutine does not work. In fact in the manual is reported that Unity automatically uses it in coroutines.
So the problem is still here: no way to start a function, inside a class, which is using YIELD instruction. This is a real BIG problem.

Yes, but only with classes that derive from Monobehaviour. You do have to use StartCoroutine explicitly if you’re doing it with your own non-Monobehaviour class. This is a true fact (and the docs should be updated to include this info).

If you use StartCoroutine, it works fine. In monark’s example code above, if you use StartCoroutine, it works. If you don’t use StartCoroutine, it doesn’t work.

–Eric

To further complicate this… StartCoroutine is a method of MonoBehavior, so you cannot StartCoroutine from any class not derived from MonoBehavior. You should be able to StartCoroutine from a MonoBehavior object to a class function, so yield would work in that instance.

I have recently run into this problem is there a way of fixing it?