im trying to destroy the chunk of blocks as quickly as they are instantiated. Basically blocks are destroyed and instantiated depending on the player’s distance from the chunk. When blocks are instantiated, they quickly instantiate as if the entire chunk in instantiating at once. However, when it comes to destroying them, it seems that they are destroyed in flowing rows, like this (may not be a good image):
So the questions is, how do i destroy the blocks as quickly as they are being instanced? Does it have to do with using a foreach loop and a list? Here’s the code:
Vector3 chunkCenter = new Vector3(transform.position.x + BlockData.chunkX / 2, BlockData.chunkY, transform.position.z + BlockData.chunkZ/2);
if(Vector3.Distance(chunkCenter,player.transform.position) > terrain.maxViewDistance) {
terrain.chunksToRender.Remove(transform);
foreach(Transform block in terrain.blockList) {
if(block != null) {
if(block.position.x >= transform.position.x && block.position.x < transform.position.x + terrain.blocksX) {
if(block.position.z >= transform.position.z && block.position.z < transform.position.z + terrain.blocksZ) {
Destroy (block.gameObject);
terrain.blockList.Remove(block);
}
}
}
}
} else if(!terrain.chunksToRender.Contains(transform)) {
terrain terrainScript = blockGenerator.GetComponent<terrain>();
terrainScript.renderChunk(transform);
terrain.chunksToRender.Add(transform);
}