# How to instantiate chunks based on time.

I’ve been working on terrain generation similar to Minecraft’s, based on cubes. And now the last problem I find is that I can’t wrap my head around how to stop all the ‘chunks’ from generating at once. This is the function that I am using to instantiate the chunk prefabs.

``````void Update ()
{
for (float x = transform.position.x - renderDistance; x < transform.position.x + renderDistance; x+= chunkWidth)
{
for (float z = transform.position.z - renderDistance; z < transform.position.z + renderDistance; z+= chunkWidth)
{
Vector3 pos = new Vector3(x, 0, z);
pos.x = Mathf.Floor(pos.x / (float)chunkWidth) * chunkWidth;
pos.z = Mathf.Floor(pos.z / (float)chunkWidth) * chunkWidth;

Chunk chunk = Chunk.FindChunk(pos);
if (chunk != null) continue;

chunk = (Chunk)Instantiate(chunkFab, pos, Quaternion.identity);
World.generate = false;
}
}
}
``````

Help would be very much appreciated.

The whole loop can be done in a Coroutine, with a `yield return null` at the end of each iteration.
This will limite chunk generation to one per frame.