Why will OnBecameVisible not work?

I am creating a game with a bunch of planes forming cubes to make terrain. I am using OnBecameVisible and OnBecameInvisible to turn the renderer on and off.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Cull : MonoBehaviour
{

void OnBecameInvisible()
{
GetComponent<MeshRenderer>().enabled = false;
}

void OnBecameVisible()
{
Debug.Log("Visible");
GetComponent<MeshRenderer>().enabled = true;
}
}

The planes become invisible, but when OnBecomeVisible is called, the script prints that it is visible again, but the meshrenderer does not turn on.

you could try setting a variable for the meshrenderer, when i tried it it didnt say visible again… so…

This forum doesn’t have markup. Use the code tags provided in the little formatting bar.

If you disable the renderer, then the system has no idea when it becomes visible again, because it’s the renderer that decides whether it’s visible or not.

How then can I disable the rendering?

whats wrong with occlusion culling?

Unity seems to not do it automatically.

no you have to click bake… and maybe mark things… its really simple

Bake?? Where do i click bake? My planes are spawned in by a script like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BlockSpawner : MonoBehaviour
{
    public GameObject block;
    public int width = 0;
    public int height = 0;
    public int depth = 0;
    void Start()
    {
        transform.position = new Vector3(-0.5f, -(depth/2), -0.5f);
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < depth; j++)
                {
                    transform.position = new Vector3((i / 2) + -0.5f, 0f, (j / 2) + -0.5f);
                    Instantiate(block, transform.position, Quaternion.identity);
                }
            }
    }
}

well instead of inventing your own, why not use the tools available? ok so your objects arent marked as anything by default, but Unity - Scripting API: StaticOcclusionCulling will set you on some path, or you could play around in a scene and see for yourself using the occlusion culling window provided to set properties and bake and the like to see what it does.

But will it work with the spawning script that spawns in plane cubes?

So you didnt even look… Im wasting my time

Yes I looked, it seemed like it needs to already be in the scene

then you didnt look…

As others have mentioned already, renderers will be culled automatically. OnBecameVisible/OnBecameInvisible is driven by these culling results, so it doesn’t make sense to use it to disable the renderer. It is meant to disable other calculations that are not needed while invisible.

You can do culling manually with the CullingGroup API, if you really want. The CullingGroup API is independent of the renderer.

However, the tricky thing is that a renderer can be seen by more than one camera - in particular the cameras that are used for shadow map rendering. So you not only have to check if the object is invisible to the main camera, you also have to check each shadow casting light (… cascade).

PS: The StaticOcclusionCulling class seems to be an editor-only helper class. Don’t think you can use that one.