When should I deactivate objects, and what is the most effecient way?

I have made a 2d sidescroller game. So I’ve been thinking if I should deactivate objects when they are outside the camera, in order to optimize the performance. So I have a couple of questions regarding this.

  • Is Unity already doing something smart, not rendering objects outside of screen, without me knowing it?
  • What’s best performance wise? Disable the object’s renderer, or toggle activeSelf?
  • Is it even more effecient to track and deactivate objects, rather than leave them be?
  • Is it best to have all objects figuring out in fixedUpdate() if they should be active or not. Or is it better to have one list of all objects, checking the currently active ones? Or a third option?

If you can answer one of these questions, please do so.

Thanks for reading.

I’m not sure about your first question; you might want to look into the new Culling Groups feature recently added in 5.2 that’s discussed a bit in the Unite 2015 keynote: Unite Keynote 2015 - YouTube

As for the rest, gameObject.SetActive(false) is the way to deactivate a gameobject (assuming you can’t Destroy or recycle them). Whether or not it’s worthwhile will depend on how many objects you have in your scene, how many of those objects have components running code in Update(), and what your target hardware is. If you’re having performance issues with the number of objects in your scene, the performance gain from turning a large portion of them off will most likely be worth the cpu cycles spent checking them.

An object that has been disabled will not run its FixedUpdate(), so you cannot check whether to re-activate it there. You’ll need some kind of external manager.