# Destroy chunked block like Minecraft

I am trying to do a Minecraft-like game (like every one have already tried I guess) and i went across the problem i expected: lag with world Generation. I did lot of searching but i can

I tried to combine them into a bigger mesh, which work, but not i can destroy a single block. So how can I place/destroy chunked (mesh combined) block? If i cant, can someone clearly explain me a solution (i am not fluent in english nor with programming expressions, i can code tho) and how to do it (with a script example if possible :) ) ? Thanks in advance !

I generated my world using 2D perlin noise. Once i have the blocks, i split them into 16x16x100 chunk meshes.

Before Making chunks, i first tried to combine all of them into one block (using the unity manual), but the result is only one cube placed at 0,0,0. Where are all the other cubes ?

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ThisWorldGen : MonoBehaviour {

public GameObject blockPrefab;
public GameObject grass;
public GameObject dirt;
public GameObject rock;
private string[] blockTag;
private GameObject stockin;

public int amp;
public int freq;
private Vector3 mypos;
[SerializeField]
private Material material;
private Vector3 blockLocations;

public List<MeshFilter> meshes = new List<MeshFilter>();

void Start()
{
Generate();
}

void Generate()
{
amp = Random.Range(0,100);
freq = Random.Range(30,100);
mypos = this.transform.position;
int cols = 100;
int rows = 100;
float startTime = Time.realtimeSinceStartup;
#region Create Mesh Data
MeshFilter blockMeshes = Instantiate(blockPrefab, Vector3.zero, Quaternion.identity).GetComponent<MeshFilter>();

for(int x = 0; x < cols;x++)
{
for(int z = 0; z < rows;z++)
{
float y = Mathf.PerlinNoise ((mypos.x + x) / freq,(mypos.z + z)/freq) * amp;
y = Mathf.Floor(y);
for(float hy = y; y > 0.0F; y -= 1.0F)
{
blockMeshes.transform.position = new Vector3(mypos.x + x,y,mypos.z +z);//move the unit cube to the intended position
}
}
}
int i = 0;
MeshFilter[] listMeshes = new MeshFilter[meshes.Count];
{
i++;
}
CombineInstance[] combine = new CombineInstance[listMeshes.Length];
int w = 0;
while(w < listMeshes.Length)
{
combine[w].mesh = listMeshes[w].sharedMesh;
combine[w].transform = listMeshes[w].transform.localToWorldMatrix;
listMeshes[w].gameObject.SetActive(false);
w++;
}
Debug.Log (combine.Length);
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
transform.gameObject.SetActive(true);
#endregion
Debug.Log("Loaded in " + (Time.realtimeSinceStartup - startTime) + " Seconds.");
}
}


(the world was generating randomly and perfectly before trying to merge the meshes)

• "Once I have the blocks, I split them into 16x16x100 chunk meshes" - how does that chunk mesh creation look? Can you add/remove a block in your source data (as though the Perlin noise had given you a different value at that point), then repeat the chunk mesh creation from source, to produce a modified chunk? Jun 5 '19 at 2:40
• I guess checking the array of block value and then deleting the one i click and update the mesh is what you mean ? Jun 5 '19 at 2:49
• Sounds like that could work. Did you have any trouble implementing it that way? Jun 5 '19 at 2:55
• Haven't really tried. I tought about something than could not work, but i dont remember what it is. Is it how minecraft display millions of blocks ? Jun 5 '19 at 2:58
• Try it, then ask here if you run into trouble. :) Jun 5 '19 at 2:59