What to use instead of return?

Hi all,

I read in another topic to avoid using “return” in coroutines.

What do you use instead to terminate a function early?

In C#, you’d use

yield break;

See also:

yield (C# Reference)

Ah, well I’m using JavaScript for now (but I’m wondering if I should switch to C#).

in JS I tried using break but I got an error saying there was no block to break out of.

It’s not a problem yet, since all my coroutines have while(1) sections in them, but I am still wonderin’ …

Not sure … in JavaScript, it might even be possible to just do “return”. If it compiles and doesn’t throw runtime exceptions, you should be fine (but I’m afraid that’s the only way to figure out because AFAIK, there is no “UnityScript specification”).

EDIT: Btw, if you do while (true) { doSomethingThatRepeads(); } a lot, you might consider using InvokeRepeating instead (just found out about that myself recently and I think it might be best for many of those repeating methods :wink: ).