- Home /
Weirdness.. Why does putting yield in function corrupt commands?
Hello. I have a couple of functions set up with a yield to wait for seconds in each. After the yield, I have some other instructions of course but, for some reason the 2nd set of instructions will not execute- or in the other function it will completely not execute.
When I take the yield out of the functions, they work perfectly?
The first function is started via InvokeRepeating. With yield, it will completely not work. The yield is no way longer then its repeat rate.
The second is in an Update function and is called on a GetButton Down command. With yield, it does execute, but I also have a
for(var foos in fooObjects){
foos.GetComponent.ladida
}
code in it as well, this part will not work with yield in the function.
Why do they malfunction with yields? I do need to have yield in them.
please help, and thank you.
Can you post more code please. There is not enough here to really answer your question without really guessing.
NOTE - I figured out the problem with the GetButton function
As for the invokerepeating, there is definitely an issue there. The script is exactly how it sounds
function Start(){
InvokeRepeating("foo",5,10);
}
function foo(){
print ("did foo happen?"); yeild WaitForSeconds(2); print ("foo did happen");
}
without yield, it will execute and repeat perfectly. With yield, it wont even print "did foo happen"
Answer by Ray-Pendergraph · Jun 02, 2011 at 06:07 PM
I have never used InvokeRepeating but according to the documentation there is no indication that the thing that is repeating should be a coroutine. In fact this behavior is consistent with a coroutine method being called directly and not being called as a coroutine. All the code up to the first yield will operate and nothing else will ever happen. That sounds like what you have described.
This is because all of the code up to the first yield is in the first 'case' of the coroutine which gets called right after the creation of the Enumerator for the coroutine. If you want a repeating coroutine that can yield just use a for loop from 1 .. 5 or something, yielding in between.