Enable Mesh Renderer during gameplay, Help needed.

Hi everyone,

I’m trying to make a minecraft based game, so it needs terrain generation and stuff…
To prevent lag because of the amount of cubes i tried to use renderer.enabled to disable the meshes that the player can’t see.(the cubes are made of planes, not just a cube), the problem is that after the mesh renderer was disabled, when i look on the disabled objct it won’t enable the mesh renderer anymore…

This is the code:

var PlaneMesh : MeshRenderer;

function Update() {
	if (renderer.isVisible) {
	PlaneMesh.enabled = true;
	} else {
	PlaneMesh.enabled = false;
	}
}

I’ve looked on the answers and searched on the forum but nothing helped me.

P.S. The terrain is generated from a prefab which is made of 6 planes to make a cube, every plane has the script attached.

Edit: Forgot to say that even if the camera is pointed on the obj. and i try to enable the mesh renderer in the inspector it won’t enable anyway…

What you are trying to achieve is called frustum culling and it happens automatically in Unity. Meaning, renderers that are not visible are not drawn. If your script would work, it would only slow down the culling process instead of making it faster.

As a side note, your code can’t work, because the visibility is only computed for enabled mesh renderers. That means, isVisible stays false as long as the renderer is not enabled.

Thanks very much!