I have run into so many of these errors it just isn’t funny.
Another example:
I make a function, calling it with a yield, everything works fine.
I make a second function, it is fine, I call a yield on the second function now the first function has become a generator and cannot return a value and cannot be used with the yield statement…
Heck, I even copied and pasted the example code from the docs into my page but because I already have a function I call yield on, the EXAMPLE CODE returns errors saying it is a generator and cannot return values…
So my question again:
What the hell is a generator???
Any function which contains a yield statement is a coroutine (which is a specialised kind of generator function). The mechanism which controls coroutines requires that they return an IEnumerator object, and the yield statement automatically returns this object for you. However, that means you can’t output your own values with the return statement and you can’t specify the type of the return value as being anything other than IEnumerator.
If you need to get a value out of a coroutine, you’ll have to put it somewhere that the calling function can get at, such as in a member variable (declared outside of all functions). Don’t forget that coroutines are designed to run over an indefinite period of time, and the variable probably won’t be set right away, so you’ll have to make sure you know when the coroutine has finished before relying on its value.