- Home /
What's an effective method for activating and deactivating gameobjects not visible to the camera?
I'm making a 2D platformer and I was wondering what method would be effective in enhancing performance by selectively activating and deactivating gameobjects within the camera's view and out of the camera's view. There are 2 methods that seem viable at the moment.
I was reading in this thread to create a giant collider around the player and activate objects that go within reach of the collider.
This would only make sense if you were activating certain components of a game object such as a script or a collider, but it wouldn't work if the entire gameobject was inactive. If the gameobject was initially set to inactive 'OnTriggerEnter2D' wouldn't fire, hence this method is effective for parts of gameobjects.
Another method was this:
http://answers.unity3d.com/questions/806660/do-offscreen-gameobjects-affect-performance.html
where you partition your game world into small parts and selectively activate all the objects present within that specific part of the game world. This seems like a decent strategy but incurs the overhead of creating more gameobjects in memory and probably more colliders to detect entering and exiting areas.
My ideal solution would be activating gameobjects that are initially inactive and the 2nd solution seems like the best solution so far. Would anyone have any suggestions or recommendations on any alternatives?
@Ali hatem @Steen Petersen Using the OnBecameInvisible() and OnBecameVisible() methods is actually really similar to the first solution I initially mentioned above. On top of that, OnBecameInvisible() and OnBecameVisible() require GameObjects to have a Renderer attached to them. I'm looking for a solution that works for any type of game object. Have any suggestions?
@jamilWinkelbaum i think OnTriggerExit2D
is good if collider size is equal camera size or near & yes use setactiv ins$$anonymous$$d of destroy will make the game 10 times faster this is called object pooling where you should instantiatethe the amoun of all necessary object & set them not active in Start()
or Awake
& use if !GameObject.activeInHierarchy to activate for better & more info see this tutorial object pooling unity official Lesson
Answer by SteenPetersen · Mar 19, 2016 at 04:50 PM
Yea Id stick to:
void OnBecameInvisible()
{
Destroy(gameObject);
}
This will grab any game object that find themselves outside of the cameras visible area and simply destroy them. Probably the easiest way to solve that issue.
Please note that when debugging this in scene view the object will remain available as long as you can see it even in scene view camera.
Your answer
Follow this Question
Related Questions
I Can't Jump, And If I Remove if(isGrounded) I Can Do More Then 10 Jumps IN The Air 1 Answer
so I'm trying to make a 2D platform but the first animation i make is the only one that registers 0 Answers
"Dash-Move" only work some times. 1 Answer
Having some stuck issues on the 2D infinite runner 0 Answers
Switch Players during play 2 Answers