Program not responding! Performance problems?

Hi guys!

(You can skip this part)
After a few months of as2, then c#, then as3, i found out i didn’t get quite where i wanted with my game. Being a fan of Minecraft i thought i would just start a similar project in unity. The only thing i dont like about it this far is that i have to draw the levels myself ( in as3 i just generated them with code ). I tried to fix this by having a simple mesh just to hold my script ( a cube that is ).

This script makes unity stop responding if i run it in its window, and does the same if i build it first aswell.

for (var z = 0; z < 100; z++) {
	for (var y = 0; y < 10; y++) {
		for (var x = 0; x < 100; x++) {
			var cube = new GameObject.CreatePrimitive(PrimitiveType.Cube);
			//cube.AddComponent(BoxCollider);
			var i = Random.Range(0.0,3.0);
			if(i < 1 && i > 0){
				cube.renderer.material.color = Color.red;
			}
			if(i < 2 && i > 1){
				cube.renderer.material.color = Color.blue;
			}
			if(i < 3 && i > 2){
				cube.renderer.material.color = Color.green;
			}
			Instantiate (cube, Vector3(x, y, z), Quaternion.identity);
		}
	}
}

}

Yes, i do realize I’m trying to generate 100,000 blocks :wink:
Anyway, how would i make unity do this without stopping to respond? is there any way i can make it load one after the other without freezing up?

There is indeed, it’s called coroutine. They stop the execution of a function for x times, let the program run, then resume the function. You seem to be using javascript, so here is what you need to do :

  • To tell the function “wait until next frame”, use the command “yield null;”
  • To tell the function “wait 5 seconds”, use the command “yield WaitForSeconds(5);”

If you’re curious about what the freak are those magical things, look over here.