How to render only the visible meshes of the blocks?

Hi, so my world generation script is working just fine but there is a problem that is causing lag and that is that when I instantiate too many objects it becomes laggy, I mean I have an area of about 40x40 blocks and it lags just then, and the reason to that is because I’m instantiating, but however, if there’s any way to display only what’s visible and not render all the other sides of the blocks then that would save a lot of performance power, but how do I do that? Do I make a script that always renders the visible content of the object and then attach that to the prefab that I’m instantiating? Or something like that?

Here’s my world generation script:

var player : Transform;
var origin : Transform;
var width_x : int;
var width_z : int;
var depth : int;
var blocks : GameObject[];

function Start () {



for(var x : int = 0; x < width_x - 1; x++){
 
    for(var y : int = 0; y > -depth - 1; y--){
    
      for(var z : int = 0; z < width_z - 1; z++){
          
        Instantiate(blocks[Random.Range(0, blocks.Length)], new Vector3(x, y, z), Quaternion.identity);
      
               
         }
         
      }
      
   }
  
}

You write a script that disables MeshRenders for GameObjects which are invisible to the camera/player.

Seems Unity already has something to detect visibility: Unity - Scripting API: Renderer.isVisible

I bet the default use of this is to hide MeshRenders when a mesh is out of range for an active camera, not whenever an object without a transparent material is in front of another mesh. But shadows should be an independent element, wonder if they are.

OR

If there is no free space between these blocks/cubes, which there wouldn’t be judging from the use of concussive integers in the posted script. Then create a dynamic object, the geometry of which is changed with added or removed blocks/cubes. I bet MineCraft clones have done it this way.