- Home /
C# yield not doing anything
I have two classes, ClassA and ClassB.
In Class B there is this method (cA is a reference to class A):
public void Entrance()
{
StartCoroutine(cA.DoStuff());
}
In Class A is the do stuff method:
public IEnumerator DoStuff()
{
Debug.Log(Time.time);
yield return new WaitForSeconds(2);
Debug.Log(Time.time);
}
Both times come out the same, even though they should be two seconds apart. Both classes are Monobehaviours and the contents of the methods work fine. They do their job, its just that it takes awhile and I would like to keep the rest of the game updating. But currently it seems the yields are just ignored, what gives?
EDIT:
DoStuff needed to be started from a method in it's own script. A dummy middleman solved that. The real problem arose when I did that. The equivalent DoStuff() in my script is called around 100 times within the same frame. This freezes the game as if you didn't even have the yield in there. Thus all the DoStuffs() have the same start and end time because they are running parallel to each other.
I could fix this with a 'yield return StartCoroutine' but this would require the middleman to be of type IEnumerator, which would bring me back to my original problem of not being able to call the method from another script.
So in order to yield you need IEnumerator and to call the method with the yield you need an IEnumerator, then to call that method... so on and so on.
How do you break this endless chain of Enumerators?
EDIT 2:
I found a workaround which does not use yield.
Still, here's a (hopefully) more specific explanation of what I am doing: There are two classes, one that generates the locations for hundreds of GameObjects and stores them in an array in the other class.
Class A is the Generator
Class B is the Renderer and handles all displaying/retrieving/manipulating of the GameObjects
Before the first update is ever called, The Generator (A) goes to work placing references to all these GameObjects and Renderer (B) begins Instantiate what the Generator says to make. This takes a long time and freezes the game during the process.
If I threw a yield every time the Renderer finished a task (each one taking around 0.2 seconds processing time), the game would not freeze. That's the situation.
I can put a yield in but the resulting Coroutine code causes there to be so many Coroutines running that it might as well not have any yields.
My workaround is fairly simple: The Generator adds the GameObjects to be Instantiated to a queue and then the Renderer makes a bunch every time Update is called.
There may not be a clean answer to this problem. I'm sorry if it's a bit hard to understand what I wrote.
DoStuff doesn't need to be started from a method in its own script. It's not necessary to have middlemen or chains of IEnumerators. You must have some other issue. Also, why are you calling DoStuff 100 times? Perhaps you should post the actual scripts, because what you wrote here works perfectly fine.
As far as I can see, only using StartCoroutine() will continue execution ins$$anonymous$$d of waiting for the coroutine to finish. To circumvent this we use yield return StartCoroutine() which requires the method it is contained in to be of type IEnumerator. Somewhere along the line one of the methods must run concurrently with the coroutine
$$anonymous$$aybe you should explain what you're actually trying to do.
So, you're doing a pile of pre-processing for stage 2 in spare cycles of stage 1? Seems like a single coroutine with a 1-frame yield would be fine.
If creating an item really takes 2/10th of a second, yow! Is there anything in common you can precompute? There's no faster algorithm?
Answer by Eric5h5 · Sep 01, 2011 at 05:15 AM
If I run those scripts (replacing "Entrance" with "Start" and creating an appropriate cA variable for the first script), then the output is:
0
2.004005
So in fact everything works as expected.
Answer by Owen-Reynolds · Sep 01, 2011 at 07:38 AM
There could be an issue with starting a coroutine in a different script. If so, can work-around by converting DoStuff into a "driver":
// in "other script" A. Users call this one with cA.DoStuff();
public void DoStuff() { StartCoroutine( realDoStuff(); }
private IEnumerator realDoStuff() { print, yield, print .... }
Yes, this is in fact the problem. Looks like Unity didn't like calling coroutines in different scripts. But this has opened another can of worms, par my edit.
@Reticulatas: No, as I said in my answer, I replicated your scripts and they work fine. Calling a coroutine in a different script is not an issue. I do it with some frequency.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Custom Yield Instruction for FixedUpdate frames 3 Answers
Stop the Awake() until a user clicks a button? 1 Answer
C#: Use of while loops inside of a coroutine 2 Answers
How to use yield within a class function 2 Answers