- Home /
Issue with yield instructions in C#.
I am attempting to re-write a lot of my scripts into C#, and I am having issues getting Yield Instructions to function how I wish them to.
No matter how I attempt to execute them, they either cause the methods they are used in to continue to the next method without being finished, or completely crashes Unity on attempt to execute the script.
I have tried:
yield return null;
yield return new WaitForSeconds(0.001f);
yield return new WaitForEndOfFrame();
yield return 0;
yield return StartCoroutine(WaitMethod(0.001f); //This would simply run the WaitForSeconds in another method.
All in locations where I would like to pause temporarily. They mostly cause Unity to just completely freeze. I do not want to pause very long, for milliseconds just so that Unity can perform as normal, as they are long scripts that take some time to execute.
I know that EVERY method I am attempting these in work normally if they are not yielded at all, but they take too long to execute in one burst and cause them to freeze the game for seconds or more, and because of how often they run it is not acceptable (for example, pathfinding or dynamic generation).
I do not want to create separate Coroutines, as one method relies on the next. The use of the yield functions is simply to pause them for a very short period so that everything else can function.
Any help on this issue would be great. I had these all working in JS, but Yield Instructions in C# seem to work differently so I'm not sure what I'm doing wrong.
Answer by TrickyHandz · Sep 20, 2013 at 03:32 AM
With C# anything with a yield needs to be within an IEnumerator (i.e. a Coroutine), so you would have to write coroutines for anything that requires a yield.
I have made all the methods that would need a yield into a Coroutine at some point during testing yield instructions, however whenever I execute the yield in that method, it causes all of Unity to just freeze.
So, for example:
IEnumerator<WaitForSeconds> $$anonymous$$ethod() {
for(some loop) {
do things;
yield return new WaitForSeconds(0.01f);
}
}
That function causes Unity to crash completely and utterly when attempted to run, compared to:
void $$anonymous$$ethod() {
for(some loop) {
do things;
}
}
This second function will run perfectly fine, but just take some time due to the nature of what it needs to perform.
As far as I know an IEnumerator does not take a generic parameter. The syntax for your Coroutine should be like this:
IEnumerator $$anonymous$$yCoroutine()
{
for(some loop)
{
//Stuff to do
yield return new WaitForSeconds(0.01f);
}
}
I read that a lot recently, and have been trying to figure out why I have been getting an error when I attempt to use just "IEnumerator".
Turns out it was because I was including System.Collections.Generic (and not System.Collections by itself, because I didn't need it), and there is a Generic version of IEnumerator. This is likely the cause of my crashing, I will test this now.
Thanks a lot.
No problem, and thanks for pointing out that there is a Generic version of IEnumerator. That is something that I'll have to look into.
Yep, that seems to have been the problem. It's not completely working yet, but it's not crashing anymore so I can work it out from here. Thanks again, much appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114512im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I use yield in C# 3 Answers
Yield not working in C# 2 Answers
Issue with yield 0 Answers
How to dispaly list of images from server on GUI? 1 Answer
Need to call yield TWICE ??? (ANSWERED) 3 Answers