- Home /
The question didn't really have a definite answer but people in the comments helped to give me the advice/information I needed.
What is the best way to manage gameobjects in a scene to conserve memory?
Hi. I'm writing a script to manage active gameobjects in a scene. What the script is supposed to do is disable/enable the object based on how close the player gets to the objects position. I have a 7 objects being managed by this script and these seven objects are distanced away from each other and has a script attached to it that makes it orbit around Vector3.zero
.
What I want to know is it better memory wise to just leave all the gameobjects enabled in the scene or to disable the gameobject and then enable it when the player gets close. The point is to conserve memory. I could just delete the gameobject and load it from Resources when the player gets close but I don't want all the lag that comes from Resources.Load
or Resources.LoadAsync
so I'm trying to avoid that. What should I do?
Code example in case anyone needs reference:
public Transform player;
public GameObject[] objects;
public float maxDistance = 60f;
void Update () {
for(int i = 0; i < objects.Length; i++){
float distance = Vector3.Distance(objects[i].transform.position, player.position);
if(distance > maxDistance){
if(objects[i].activeSelf)
objects[i].SetActive(false);
}else{
if(!objects[i].activeSelf)
objects[i].SetActive(true);
}
}
}
Indeed destroying and creating will not be a good practice. $$anonymous$$emory wise it will take more to manage the enable/disable state (you have some extra references and stuff) than just leaving them on, but it is just negligible. You will gain performance, but first make sure if you have to optimize that part of the game, because I doubt it is that much worth optimizing it. If you do have performance issues, look where the problem is (use deep profiling for example). Or lower the quality of the shaders/shadows/models or use LOD. If you have to optimize the part that you describe yeah turning them off when they are far away disable them, make sure you do not use magnitude but sqrmagnitude because it Sqrt is quite heavy and unnecessary. SetActive(transform.localPosition.sqr$$anonymous$$agnitude < (_sqrDistance)); // where _sqrDistance is cached -> _sqrDistance = _distance * _distance
And are these 7 objects doing something very processor-intensive when they're enabled? Because if not, like @hoekki says, this sounds like a case of premature optimisation.
The seven objects are planets I have orbiting a star and rotating on their axis. The star is just a Lens Flare and the planet models are extremely simple.
In that case I highly recommend you do nothing at all and let Unity do its automatic management tasks (such as culling renderers that aren't in view, putting rigidbodies to sleep etc.). With so few objects, the overhead of your manual management system is almost sure to exceed any possible benefits it would offer.
Could you share a screen shot of the code? I cant help unless i know how this is being accomplished. As long as its c#
I have to agree with the above ppl. Your way below the line for time to start optimizing. And honestly, unity does a good job of doing it in tbe background anyway. Just a thought, id change the void update to FixedUpdate, it'll run smoother if you do ever get to the point where this code becomes useful in the game. Hope that helps.
Thanks everyone for the advice. It really helped to better my understanding of what to do.
$$anonymous$$emory wise once the object is there in the scene, it is in memory. Enabling or disabling that object does not add and remove it from actual memory, thus the entire concept behind your script is pointless. On top of that, Unity GameObjects are very small (we are talking bytes here) and an optimization at that level is almost certainly not going to make an impact on your game.
There are a handful of VERY specific optimization techniques in which people are managing memory in such a way to never run garbage collection (there was a Unite talk from the Playdead guys who used a technique like this for their title Inside), but optimizations at this level for memory purposes alone are typically futile.
You can confirm all of that by using the profiler, which is something I would suggest you spend some time getting comfortable with so that when you do need to optimize, you can better find where to make those optimizations.