Coroutine makes unity to freeze till it is done
Hey! Why would a coroutine make Unity to stop responding till it finishes? So i have a MapGenerator script at the Start() a coroutine is called StartCoroutine(GenerateMap()) the GenerateMap() calls several heavy functions (many while loops running) and takes several seconds to finish so the idea was to let it run at several frames till it finishes its job but it makes Unity to stop responding till it is done. Any ideas what is going wrong or any suggestion to go around that? There is nothing else called out of the GenerateMap() only an OnDrawGizmos() but it doesn't seem to cause the problem. The code is fairly large so i won't post it.
Answer by hexagonius · Nov 23, 2016 at 05:28 PM
Calling a coroutine is only the first step, since it's also running on the main thread. For the engine to hand it some execution time you need to call a yield every now and then eg:
yield return null; //gives Unity one frame
yield return new WaitForSeconds(xSeconds);// as many frames as Unity managed to execute
in essence you'll have to run these after x execution steps.
Your answer
![](https://koobas.hobune.stream/wayback/20220612101248im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# Pause Menu Buttons not working in only 1 scene 1 Answer
Execute coroutine in Update() 8 Answers
yield ends method 0 Answers
using coroutines 1 Answer
Coroutines Madness (); 1 Answer