How to disable objects that are outside viewport?

Hi,

I tried hard to figure out how to hide whole gameobjets with sprite renderer for performance reasons. Disabling only the sprite renderer does do nothing with performance. I need to disable the whole object that is not seen by the Main Camera and activate it again when seen.

My problem is that I have a cript using update method and check if the object is visible by

bool visible = GetComponent<Renderer>().isVisible;

but when the object is deactivated, Update is not called anymore so the object is never set active again when in the viewport…

The script has to be really simple. Sould not be using update because I have a 2D platformer and my scene is build up from hundreds of sprites. I need all sprites turn off when they are not visible.

Maybe you can estimate the coordinates that are outside the viewport, and then say

for (int i = 0; i < gameObject.getGameObjectsWithTag("disable?"; i++)
{
  if (transform.position.x < camera.transform.position.x - [outside the camera])
  {
   //disable the object
  }
  if (transform.position.x > camera.transform.position.x + [outside the camera])
  {
   //disable the object
  }
  //... (the same with y axis)
}

This is just so you take it as an idea, obviously not real code :slight_smile: