title: Display progress in a long loop in Unity3d
body: <p>I'm building a space game, populated by thousands of star systems. Right now, there is a function that generates these thousands of systems. It takes about 5-10 seconds to generate, and I would like to have a simple progress bar updating the user in the progress.</p>
  
  <p>After some searching I've decided to use a coroutine, however there is a little problem: when I call the function to generate star systems, the code that called the function keeps on going, reporting zero stars (because they haven't been generated yet).</p>
  
  <p>I have a feeling coroutines are not an answer for me. Basically, I am simply looking for a way to simulate Application.DoEvents to update the GUI.</p>
  
  <p>Here is a sample of my code:</p>
  
  <pre><code>// Start generating thousands of systems
  StartCoroutine(GalacticMap.GenerateRandomGalaxy());
  // after the universe is generated...
  Game.myGalaxy.StarSystems = GalacticMap.myGalaxy.StarSystems;
  // report back the number of systems
  print(String.Format("Generated {0} systems", Game.myGalaxy.StarSystems.Count));
  </code></pre>
  
  <p>In the <code>GalacticMap.GenerateRandomGalaxy()</code> I am yielding back like this <code>yield return new WaitForSeconds(0.1f);</code> however, the effect is not what I am looking for: execution goes right through to <code>print</code> statement while the generation is still on going. </p>
  
  <p>So how do I do this?</p>
  
  <h1>EDIT 1:</h1>
  
  <p>I've cooked up  a sample code to illustrate my issue.</p>
  
  <p>The caller code:</p>
  
  <pre><code>Debug.Log ("Start generating");
  StartCoroutine(GenerateMeGalaxy());
  Debug.Log ("Finish generating");
  </code></pre>
  
  <p>The code I call:</p>
  
  <pre><code>public static IEnumerator  GenerateMeGalaxy ()
  {
      Debug.Log ("GenerateMeGalaxy start");
      int numberOfStars = 1000;
  
      for (int i=0;i&lt;=numberOfStars;i++)
      {
          // generate galaxy
  
          // display progress bar on screen
          Debug.Monitor(String.Format("{0}% completed", ((i*100)/numberOfStars)),2);
          yield return new WaitForSeconds(0.001f);
      }
      Debug.Log ("GenerateMeGalaxy end");
  }
  </code></pre>
  
  <p>In the code above, <code>Debug.Log</code> displays string on the screen, on a new line so you can read previous logged strings of whatever. This is to debug code execution. <code>Debug.Monitor</code> has a fixed location on the screen and will overwrite previous string. This is where I display progress percentage. What I see after running this code is:</p>
  
  <pre><code>Start generating
  GenerateMeGalaxy start
  Finish generating
  GenerateMeGalaxy end
  </code></pre>
  
  <p>What I want to see is:</p>
  
  <pre><code>Start generating
  GenerateMeGalaxy start
  GenerateMeGalaxy end
  Finish generating
  </code></pre>
  
  <p>... and also the progress update is happening in between:</p>
  
  <pre><code>GenerateMeGalaxy start
  Finish generating
  </code></pre>