# World Spawn Changing Y to own Block

I am using the following script to create my world with a little help I have got it set up to spawn my blocks in all directions. The problem I currently have is that I need the Y to use its own blocks.

``````using UnityEngine;
using System.Collections;

public class WorldSpawn : MonoBehaviour {

public GameObject block1;
public GameObject block2;

int worldWidth  = 10;
int worldHeight  = 10;
int worldThickness = 4;
float spawnSpeed = 0;

void  Start ()
{
StartCoroutine(CreateWorld());
}

IEnumerator CreateWorld (){

for(int x =0; x<worldWidth; x+=1) {
yield return new WaitForSeconds(spawnSpeed);

for(int z =0; z<worldHeight; z+=1) {
yield return new WaitForSeconds(spawnSpeed);

for(int y =0; y<worldThickness; y+=1) {//This is the one that I need to make use a differnt block.
yield return new WaitForSeconds(spawnSpeed);//This is the one that I need to make use a differnt block.

GameObject block = Instantiate(block1,block1.transform.position, block1.transform.rotation)as GameObject;

block.transform.localPosition = new Vector3(x, y, z);

}

}
}

}
}
``````

Forgive me if I still misunderstood, but it seems to me that since you’re looping through x, y, and z - you’ll just have to check what the Y value is and Instantiate the block based on that since you’re building the world a column (up/down) at a time, then a row (on the z axis) across the x axis.

So you could do something like this, assuming that the top layer is block1 (or grass) and everything below the first block is block2 (or dirt)

``````IEnumerator CreateWorld (){

for(int x =0; x<worldWidth; x+=1) {
yield return new WaitForSeconds(spawnSpeed);

for(int z =0; z<worldHeight; z+=1) {
yield return new WaitForSeconds(spawnSpeed);

for(int y =0; y<worldThickness; y+=1) {
yield return new WaitForSeconds(spawnSpeed);

if (y==worldThickness-1){ //when Y = the highest layer, do block1
GameObject block = Instantiate(block1,block1.transform.position, block1.transform.rotation)as GameObject;
} else {
GameObject block = Instantiate(block2,block2.transform.position, block2.transform.rotation)as GameObject;
}
block.transform.localPosition = new Vector3(x, y, z);

}

}
}

}
``````