Full Object Culling

Does anyone know the best method for culling an entire object, renderer and scripts included based on it’s distance from the player/camera?

Right now I’m making a 2d platformer with 3d graphics, but what I want is for all the objects off screen to not play scripts or render. My Current method is attaching something like this to each object:

var closeDistance = 50.0;
var sqrLen = (b.transform.position - transform.position).sqrMagnitude;
	if( sqrLen < closeDistance*closeDistance )
	{
		if(!renderer.enabled)
		{
		renderer.enabled = true;
		enabled = true;
		}
	}
	else
	{
	if(renderer.enabled)
	{
		renderer.enabled = false;	
		enabled = false;
	}
		
	}

I want to disable everything since there is no AI that has to be calculated. I feel like there HAS to be a better way to manage this then to have every object call this every tenth of a second.

One method I’m thinking of doing is attaching a collider/trigger area onto the camera the disables of enables everything inside or outside of it. Any method that people know works really well?

Thanks!

the easiest for distance based culling is using the layerCullDistance of the camera and usage of corresponding layers :slight_smile:

See the iOS examples - warehouse to see it in action

Thanks dreamora you’ve been really helpful today.

Will this disable the scripts, colliders, etc inside the objects too, not just the renderer?

culling is only about rendering, it does not impact the rest (as it otherwise will never become active again without you doing it through code again)
Keep in mind, inactive game objects and components can also not be found with Find(xxx) etc anymore

What if you had a big object that they were all contained in that activated and deactivated based on distance? instead of each object checking? I should rename this thread.

What is the best for optimization? Put a script on each objects for culling, or put a script on the camera, that will look for each object?

Could you try “renderer.isVisible”. have your script only run if renderer.isVisible is true.