- Home /
Ideal Dynamic Object Generation Technique
I am making a game which would require objects (or civilians specifically) which would need to be generated around player.
Currently, I am using a custom system where I am spawning them randomly around the player as he walks upto a certain number. The civilians (non-important ones) are destroyed once the distance between them and the player goes beyond a certain limit. For that, I have every civilian object occasionally check the distance between itself and the player. The number of civilians is somewhere around 100-120. The issue is it can be something of a performance hog.
There is also an occasional problem where if I keep the "maximum distance" between player and civilian before it gets destroyed too large, I run into empty sections of the city.
What I want to ask is that is there a better dynamic object generation that can deal with both the above mentioned issues.
Can you teleport the civilians, hence recycling them, ins$$anonymous$$d of destroy->instantiate?
Answer by Doeko · Sep 12, 2013 at 01:59 PM
You don't want to dynamically generate objects at all, because destroying and instantiating is very slow especially if you have 100+ objects. Create a pool of Civilian objects which you re-use. So when a certain civilian goes out of range, you teleport it somewhere in the vicinity of the player when appropriate, instead of destroying it and instantiating a new one.
For further performance increases you may want to group civilians so that groups of 5-10 never wander too far from each other, and check the distance to the player as a group instead of individually.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114020im_/https://answers.unity.com/themes/thub/images/avi.jpg)