- Home /
Scene optimization: Removing/Recreating gameObjects
Loads of questions, but first the plan:
I have like 1800 of the same gameObject on scene, which have a neccessarily a big script each. I plan to Destroy() the game objects in the OnBecameInvisible() call and respawn them on OnBecameVisible(). But then these questions pop out:
How do I store all these game objects to later spawn them again so nothing is lost?
I was planning on using an array, but, if so, how do I update the array continuously without overloading the CPU?
How do detect the camera movement?
Thanks in advance.
Juan, generally, arrays are rubbish .. don't use 'em! use List ins$$anonymous$$d
for pool handling generally ... consider huge post here
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
regarding handling the objects, it's not clear what you mean. you can just disable them if you want to.
often in games you very simply move them offscreen, and leave them there until needed.
"destroy and respawn" ... you definitely should not do that.
generally never Instantiate DURING PLAY of a video game.
instantiate is way slow. You only really use it when a scene launches.
SetActiveRecursively to turn off your object when you are not using them. Have an active and an inactive List perhaps, move them from one to the other. Or you could just disable your script when the object is invisible - Unity will be culling the objects anyway in terms of rendering - this is easier because the object will get the OnBecameVisible which it won't if its disabled or been destroyed.