Screen doesnt refresh until after loop

I have some issues… (well allot really because I’m a noob) but.

In this script i am creating / adding text line by line to a text file.
and its a big loop
im trying to figure out why it wont display my progress bar each time it goes thru the loop. it waits to show the scene until after the script finishes. how do i get it to show the scene and refresh it each time i go through the loop?

I’m not sure which part of the code is the problem so I’ll just past the whole script(sorry its a mess)

another huge problem is that it takes forever to go through the loop. i think its because of the way im writing the file?

Also I have another issue and that is for some reason the file its outputting “world.dat” is only currently writing the last x,y pos. it was working but i screwed it up some how. the file just contains:

10,10
10,10

also is there a way to run my game/code one line at a time so i can watch the code in the script as it goes?

to help make sense of this madness here is how its run:
first it goes to Start() and runs the CheckForFile method to see if the file and directory exists to store the data to.

then it goes to the CreateWorld method which basicly contians my main loop

once it gets there it calls TileSelector method which is where i will figure out which tile to store in the file.

the it goes to FileIO method
this is where i write the current x,y pos and the tile type.

then it calls the ProgressBar method to display the overall progress of the file.
here is a picture of what that looks like

link text

then in the CreateWorld method if checks to see if the file is done writing yet based on current x,y. if it is it ends the program at the DoneCreatingFile method.

anyways, here is the script.

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Text;

public class WorldGen : MonoBehaviour {
public GameObject ProgBar = null; //Progress Bar Prefab
static string fileNameCheck = "World.dat"; // name of file to store the tilemap & other varibles on each tile
static string dirCheck = Application.dataPath + "/Data/"; // directory where to store data file
static string pathCheck = dirCheck + fileNameCheck; // location of data file
static uint currentXpos = 1; 
static uint currentYpos = 1;
public static float xSpawnSpot = -3.098634f; //starting spawn location of prog bar in scene
public uint worldWidth = 10; // x value
public static uint worldHeight = 10; //y value
static float progBarCount = (worldHeight * 0.037f); // current value of total progress complete

//density floats represent precentages. ex: 0.0f = 0 percent chance of that tile type to spawn. 100f = 100 percent
public float grassDensity = 75f; 
public float dirtDensity = 30f;
public float waterDensity = 20f;
public float snowDensity = 20f;
public float snowMountainDensity = 12f;
public float caveDensity = 3f;

private string tileTypeName = ""; //name of current tile
private int tileTypeNum = 0;  
/* 0 = nothing
   1 = grass
   2 = dirt
   3 = water
   4 = snow
   5 = snow mountain
   6 = player house
   7 = cave
*/

	// Use this for initialization
	void Start () {

		this.CheckForFile (); //check to see if file & directory exists
		this.CreateWorld ();
	}	

	private void CreateWorld() { // loop that generates each tile							

		this.TileSelector();
		this.FileIO();
		this.ProgressBar();	

		if (true & currentXpos == worldWidth && currentYpos < worldHeight)// add one to Y if its not maxed height
		{
			currentYpos += 1;
			currentXpos = 0;
		}
		if (currentXpos < worldWidth)// add one to X if not maxed width
		{
			currentXpos += 1;
		}	
		if (true & currentXpos == worldWidth && currentYpos == worldHeight)// if both meet max then finish file
		{
			//run these one last time then end file creation.
			this.TileSelector();
			this.FileIO();
			this.ProgressBar();	
			DoneCreatingFile();
		}
		else
		{
			this.CreateWorld(); // not done yet, start loop over again.
		}
	}		



		public static void CheckForFile() {
		if (Directory.Exists(dirCheck)) //check for dir first
		{
			Console.WriteLine("That path exists already.");			
		}
			else
			{
			// Try to create the directory.
			DirectoryInfo di = Directory.CreateDirectory(dirCheck);
			}
		if (!File.Exists (pathCheck)) 
		{   // If file does not exist then create one
			// Create a file to write to. 
			string createText = "Game World Data File." + Environment.NewLine;

			File.WriteAllText (pathCheck, createText);
		}
			else
			{
			File.Delete(pathCheck);

			}
						
		}

		//Progress bar fill
		void ProgressBar() {

		if (currentYpos >= progBarCount) 
		{			
			ProgBar = Instantiate(Resources.Load ("Prefabs/BarPiece"),new Vector3(xSpawnSpot,0.2693692f,3f),Quaternion.identity) as GameObject;	
			progBarCount += (worldHeight * 0.037f);
			xSpawnSpot += 0.246417f;
		}
			UnityEngine.Debug.Log ("Progress Bar Location: " + progBarCount);
		}
		
		void Update() {

		}


		void TileSelector (){ // figures out which tile type to generate at current location based on density percent set by that varible
			UnityEngine.Debug.Log ("Tile Selector");
		}

		void FileIO() // write each x,y location to file World.dat
		{	
			UnityEngine.Debug.Log ("y=" + currentYpos + "; x=" + currentXpos);
			// write first lines to file
			if (currentYpos == 1 && currentXpos == 1) 
			{
				string firstLineText = "Map tileset data file." + Environment.NewLine;
				string lineBrakeText = "******************************************" + Environment.NewLine;
				File.WriteAllText(pathCheck, firstLineText + lineBrakeText);
			}
			//write each x,y to file
			File.AppendAllText(pathCheck, currentYpos + "," + currentXpos + Environment.NewLine);
		
			// Open the file to read from. 
			//string readText = File.ReadAllText(pathCheck);
			//Console.WriteLine(readText);				
		}

		void DoneCreatingFile() {
			Application.Quit (); //end program
		}
}

Unity is a game engine.

Game engines run in an Update/Render loop where each frame Update is called, followed by Render.
Render is what changes the screen contents.

Nothing will change on screen until after your Update returns and your Render gets called.

Start is actually called even before your first Update so again,. nothing gets drawn until Update is called and then
Render is called.

You should start CreateWorld as a Coroutine so that the game can continue while this heavy operation takes place. If CheckForFile is another heavy operation use coroutines for that as well. For more info on coroutines see this.

Ok I tired the Coroutine. And it isn’t working. I’m not sure why.

I also put my prefab Instantiate right in the Update() just to see if it would draw. and it still doesn’t! it waits till the loop is done… wtf… ive spent way to much time on this :frowning:

I put it in my Progress Bar method… thinking as soon as its done drawing it would goto update and display it… but no

I did fix the FileIO() problem though.

here is the code for my Coroutine

//Progress bar fill
		IEnumerator ProgressBar() {

		//progress bar
		if (currentYpos >= progBarCount) 
		{	
			if (progBarPieces < 27)
			{
				ProgBar = Instantiate(Resources.Load ("Prefabs/BarPiece"),new Vector3(xSpawnSpot,0.2693692f,3f),Quaternion.identity) as GameObject;
				UnityEngine.Debug.Log("Bar Draw");
			}	
			if (progBarPieces == 27)
			{
				ProgBar = Instantiate(Resources.Load ("Prefabs/LastBarPiece"),new Vector3(xSpawnSpot,0.2693692f,3f),Quaternion.identity) as GameObject;
				UnityEngine.Debug.Log("Bar Draw");
			}
			progBarCount += (worldHeight * 0.037f);
			progBarPieces += 1;
			if (progBarPieces < 27)
			{
				xSpawnSpot += 0.246417f;
			}else 
			{
				xSpawnSpot += 0.1232085f;
			}
			
		}
		UnityEngine.Debug.Log ("Progress Bar Location: " + progBarCount);		
		yield return null;	
	}