- Home /
Freaky - can't return; out of an IEnumerator
I had some code like this:
beginSomething( 3.141, aCallBack )
function aCallBack():void
{
_aCallBack();
}
function _aCallBack():IEnumerator
{
code
code
code
return;
}
Bizarrely, perhaps the latest version of Unity, the "return;" at the end makes it throw an exception.
If you delete the return; .. no problems.
Does this mean anything to language experts?
Relatedly, I've always wondered what is the "correct" default return from :IEnumerator? I tried return null; but no good.
Thanks for any insight ...
return should work just fine in js. C# requires yield break ins$$anonymous$$d though. what error are you getting?
$$anonymous$$eep in $$anonymous$$d that you need at least one yield inside of a IEnumerator function or it won't be a coroutine. Your pseudo code doesn't show one ;)
Ah .. fascinating insight ... thank you ...
Do you think it could be the case that IF you use "return;" at the end, you $$anonymous$$UST use yield at least once ??
But at the same time, if you have a path with NO yield, then it does "let you get away with" including a return; at the end?
Answer by Bunny83 · Jun 10, 2013 at 08:17 AM
Keep in mind that you need at least one yield inside of a IEnumerator function or it won't be a coroutine. Your pseudo code doesn't show one ;)
IEnumerator is just an interface, so the function returns an object that implements this interface. The yield statement is just a simple way to create such a class "on the fly" (i don't mean on the fly at runtime but by the compiler). If you don't use yield the compiler won't turn that function into a generator function because it's a normal function which should return an IEnumerator.
Since i don't use UnityScript this is the point where it's not clear what the compiler does since it calls StartCoroutine "automatically" for you. An IEnumerator is just an object, so if you have a reference to such an object you could return it.
For example:
//C#
// this is a generator function
IEnumerator SomeGenerator()
{
yield return 1;
yield return 2;
yield return 4;
}
// NOT a generator since there's no yield.
// That means the function has to return an IEnumerator itself.
IEnumerator SomeNormalFunction()
{
// Do "normal" stuff
// calling SomeGenerator will return an object which you could just pass along as return value
return SomeGenerator();
}
This is how "yield" works in general. Since UnityScript implicitly calls StartCoroutine i'm not sure how the compiler would handle such a case.
Ahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh.......
holy shit, why doesn't the doco just say "IEnumerator is an interface"
Thank you so much !! :O
:) That's btw the default convention for interface names to put an additional I infront of the name.
The actual interface is extremely simple, but very powerful: IEnumerator
"Since UnityScript implicitly calls StartCoroutine i'm not sure how the compiler would handle such a case."
Right - there's definitely a mild woe or problem if you have, in Unityscript, a situation where the function has two paths and one path has a yield and the other doesn't. It's just the woe this guy was having for example ...
http://answers.unity3d.com/questions/321309/yield-causing-function-not-to-be-called-on-multito.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613103844im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How Return Or Restart A Coroutine When A Variable Increase? 1 Answer
How to use Coroutines in C++/CLI? 1 Answer
Instantiate Particles, WaitForSeconds and continue 1 Answer
Execute IEnumerator In 1 Frame 4 Answers
How to attach two diff. materials (we can see first mat for two seconds, than another) 1 Answer