- Home /
inactive object or unseen object
I think you may choose either making object inactive or moving object out of screen when you hide objects. I dont want to use Instantiate and Destroy for frequent-used objects like bullets. I heard inactive objects still reside in memory, so I wonder which one takes more cpu usage or memory. Can anyone answer this ? Thanks.
Answer by Hanoble · Jan 13, 2017 at 02:27 AM
When you are talking about memory optimizations at the level of removing a gameobject, such as a bullet, you are almost certainly making a premature optimization or focusing on the wrong optimization.
It sounds like you are talking about pooling objects, which is a widely used concept on mobile to help maintain FPS by not constantly doing slow Instantiate calls and simply reusing common objects (such as bullets). The idea is to shoot a bullet, once it is done (hit something or timed out) you put it into an inactive state, disable it, and then put it back into the available pool. Most pools will have an instantiate similar to that of Unity, but instead of actually making the object you simply grab the one already there, move it, initialize it, reactivate it, and let it go as if it were brand new.
Inactive objects do still reside in memory, although gameobjects are typically very small and you are unlikely to see a major benefit there. Inactive objects do not call their update functions (which could save some CPU time), but an active bullet offscreen would still consume those CPU resources (however Unity is smart enough to not render it and take up your GPU resources in most cases).
If you really want to understand the CPU and memory usage of your gameobjects, take a look at the profiler and you will find answers to your questions.
Sounds like each method has its pros and cons. I may have to choose one depending on the situation. Thank you so much for your detailed and educational explanations!!!
You are welcome, if it helped you please upvote :)