How can i make an object invisible when a camera is not looking at it, but visible when it is.

So for optimizing my voxel box game I want to be able to make the object invisible when the camera is not looking at it, but visible while looking at it. I know of the OnBecameVisible and OnBecameInvisible functions, which i can put into a code like this:

function OnBecameVisible (){
renderer.enabled = true;

function OnBecameInvisible (){
renderer.enabled = false;

But as expected re-enabling the renderer using the OnBecameVisible function will not work because there is no renderer to be seen…
Is there any way to make what I want possible?

It sounds like what you want to add is View Frustum Culling. But Unity already has this enabled by default, so I’m not sure you’ll be gaining anything from enabling and disabling renderers manually on top of what Unity’s already doing internally.

I feel like most voxel games use a more complicated method of turning an area of cubes into a single “ground” mesh. But…

To use your approach, you could turn off the renderer when a live block is on all four of your sides, and above. Whenever a block is added or removed, redo the math for the five blocks it touches.