- Home /
yield WaitForSeconds() causing compiler errors
I have two classes:
class SubScreen extends MonoBehaviour
{
function Open()
{
SetShow( true );
RandomUtils.ToggleVisibility( gameObject );
}
}
and
class SubScreen_AdvGameOver extends SubScreen { override function Open() { super();
yield WaitForSeconds(1);
// looking to do some more stuff down here...
}
}
However, this causes a compiler error: BCE0101: Return type 'void' cannot be used on a generator. Did you mean 'IEnumerator'? You can also use 'System.Collections.IEnumerable' or 'object'.
I have used yield WaitForSeconds() in numerous other places and it has worked fine. I've looked up to see if anyone else has had problems, and I found things like calling the function from Update() and OnGUI() -- The Open() function is not called from either of those. I can declare other functions in the child class that are allowed to have a yield in them, and the compiler does not throw any errors.
Under what circumstances can you yield in a function?
It looks like the super() call in the override function may be to blame for the compiler error -- it's possible to call the yield in the parent function, but only if there is no override function...but I do not understand what/why the issue is.
Answer by Joe 22 · Apr 13, 2011 at 11:07 PM
I think I may have found the answer, but it would be great if someone a little more technical could explain what is going on...
The following code fixes this and works:
class SubScreen extends MonoBehaviour
{
function Open() : IEnumerator
{
SetShow( true );
RandomUtils.ToggleVisibility( gameObject );
}
}
and
class SubScreen_AdvGameOver extends SubScreen { override function Open() : IEnumerator { yield super.Open();
yield WaitForSeconds(1);
// looking to do some more stuff down here...
}
}
By the nature of the compiler error, I suspected that the functions had to return IEnumerator (even though other functions I have used yield in return void). However, the tricky part was then figuring out to yield super.Open(). Trying yield super() does not work... I'm not clear about why the syntax is the way it is, or why the functions had to return IEnumerator despite other functions using yield and not having to.
Hi Joe, I think I have a similar problem: link text. I have added the return type EInumerator to my functions but then the coroutine is never executed...
Your answer
Follow this Question
Related Questions
Mysteries of yield 1 Answer
A delay/yield/wait function for function Update? 6 Answers
Create a 10 second delay using WaitForSeconds 2 Answers
Why does this not work? 1 Answer
WaitForSeconds not running 2 Answers