Optimizing cube terrain generator

My issue is one purely that i have no clue how to make it more efficient.
If i try to generate a terrain of substantial size it just takes up to much memory.
and takes over 4 minutes to build.

the script is attached to a home object. witch than move to a coordinate instantiatea a cube than returns back.
repeat a few hunderade times.

 var worldblock:Transform;
 var worldblocktwo:Transform;
 var worldblockthree:Transform;
 var tree:Transform;

var grass=1;
static var seed : int;
var treefequense = 0;
var xnumber = 1;
var ynumber = 1;
var znumber = 1;
var ynumbermin = 1;
var xnumbermax = 10;
var ynumbermax = 10;
var znumbermax = 10;



	function Update () { 
var oldxnumber = xnumber;
var oldynumber = ynumber;
var oldznumber =  znumber ;
var ynumbermax2 =ynumbermax;
      
        while(znumber <= znumbermax){
      //var ynumbermax2 = Random.Range(ynumber, ynumbermax);
//print(ynumbermax2);
        
        
        while(xnumber<=xnumbermax){
        while(ynumber<=ynumbermax2){
         transform.Translate(xnumber, ynumber, znumber);
     
         //var otherVector : Vector3 = Vector3(xnumber,ynumber,znumber);
         //var treeee = Instantiate(tree, position.otherVector,Quaternion.identity);
 
   if(ynumber==ynumbermax2){
seed=Random.Range(1, 12);

var treefequensetwo=Random.Range(1, treefequense);
 if(treefequensetwo== 1){
 var treee = Instantiate(tree, transform.FindChild("blook").transform.position,Quaternion.identity);
 
 }
 
//print(seed);
if(xnumber % seed){
 var bullit = Instantiate(worldblock, transform.FindChild("blook").transform.position,Quaternion.identity);
 
 
 
 
//print(ynumbermax2);



 }else{
 var bullitt = Instantiate(worldblockthree, transform.FindChild("blook").transform.position,Quaternion.identity);
 } 
  
 }else {
 var bullittt = Instantiate(worldblocktwo, transform.FindChild("blook").transform.position,Quaternion.identity);
}


transform.Translate(0-xnumber, 0-ynumber, 0-znumber);
			
 ynumber++;}
 ynumbermax2 = Random.Range(ynumbermin, ynumbermax);
 ynumber = oldynumber ;  
 xnumber++;}
 xnumber = oldxnumber;	
znumber++;}


}

Block worlds are best created with single elements. Say you want 10 different types of blocks (like minecraft) and want to place each of those block types in teh world. You will need 10 array’s of position’s to make it work. You will need 24 * the length of the array in vertices, uv and normal information. You will need 36 * the length in triangle information. Since you know which position is one is it is easy to figure the mathematical baseline of the triangle and vertex information. Simply then create vertices and faces along with uv and normal information for each block. Assemble it on to one mesh and call it one object.

Now, we have converted 10 textures to 10 objects that take up 20 draw calls… much better then 30,000 objects.

You might want to look into the Mesh.Combine function. It’s pretty quick and efficient, and I find it’s a good way to turn n-dimensional data into meshes. Save out a mesh for each side of the cube and then filter each voxel on each side to see if it should build that face into the world. Voxels which touch other voxels on every side will be ignored, and voxels which touch no other voxels will have each side added to the mesh. If you’re using just one type of object, use a boolean[ ][ ][ ], if you’re doing multiple types use a short[ ][ ][ ] (using ints is gonna make your data really freaking huge if you are saving this stuff out)

Edit: Oh yeah, if you use Mesh.Combine, make sure you Destroy the mesh when you quit the game, change scenes, or unload the data! User-built meshes don’t destroy themselves, so you’ll run into a huge memory leak if you’re not careful.

Edit2: Another suggestion, don’t instantiate every object. Read the data, and then discard it. Instantiating objects all at once is what is probably making your building take so long. The game Infinite Ocean in my portfolio is able to build and unload generated meshes dynamically, and it’s quick enough and memory efficient enough to run on mobile.

i guess i’m just not experienced enough, but i have no clue how to do any of your suggestions

I’m still lost do you have any example codes i can look at or something along those lines.