- Home /
How to optimize usage of many GameObjects?
I am creating a game in which player have to interact with many gameobjects (Mushrooms, trees, rocks, even tufts of grass). There is no problems with rendering, because i use layerCullDistances, but there is still many gameobjects. Maybe i have to save all gameobject positions in List and instantiate them when player are near? Or is there other way to do this?
I'm not sure how your game is set up, but you could try disabling them when they're a certain distance from the player.
Answer by axldoyle · Aug 27, 2015 at 09:37 AM
If I'm understanding your question something along the lines of object pooling could be helpful. This allows you to create lists of objects that can be actived/deactived on demand, it also has less of an impact than instantiating and destroying obejcts.... link text
Answer by Ches81 · Aug 28, 2015 at 02:38 PM
Instantiating objects while the user is playing is not a good idea, especially if there are alot of them. The allocation and deallocation of memory can cause hiccups while playing since the garbage collector has to kick in more frequently. A good way to avoid this problem is to use pooling, like axldoyle said. I recently wrote an blog article about that topic. Maybe this can help you finding a solution. http://blog.christianhenschel.com/2015/08/28/pooling-part-1-optimize-your-game-performance/#more-274
At once needed in the scene? I don't think so. Pooling is not just activate and deactivate GameObjects in the scene. If you use pooling, you create only a bunch of objects and reuse them. If an object isn't needed anymore you put it back into the pool until it is used on a different location. Everytime you pull an object out of the pool you have to set it's position, rotation etc. to the currently needed position/rotation.
In my scene there are trees, stones and other gameobjects, that have their positions. And i always need them (until they are far away from player).
Wow ok, that's a lot of stuff. But you don't seem to have any problems with your application's performance, so why you bother?^^
Your answer
