- Home /
GameObject-Pool to avoid performance issues in Instantiating many objects?
Can someone explain how GameObject-Pool works in avoiding performance issues using Instantiate
for many object?
Ina, you haven't seen my immense explanation ?
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Answer by aldonaletto · Dec 18, 2011 at 12:56 PM
Instantiate and Destroy are heavy operations: the system must allocate a lot of memory blocks to instantiate a single object, and free these blocks when destroying the object. This takes time, and also causes more frequent garbage collections (the internal frame-rate-killer operation that "cleans the house", "pulling" active memory blocks to the beginning of the heap).
When using some kind of GameObject pool, frequently used objects like missiles, projectiles, decals etc. may be pre-instantiated at the beginning, deactivated and stored in the pool. When launching a missile, for instance, you actually activate a pre-created missile, move it to the spawn position and launch it. When the missile explodes or disappear in the distance, it's deactivated instead of destroyed, returning to the pool for future use. This save lots of memory manager operations, improving the performance - and matching the ecological recycling hysteria of these days too!
But things may not be so easy: a generic GameObject Pool implementation may be more expensive in some cases than the Instantiate/Destroy alternative. Take a look at this article from Von Lehe: it contains a generic GameObject Pool implementation with code, project and examples, as well as several user comments pointing pros and cons.
This is really helpful, wish information like this was in the docs so it was easier to find. Thanks
This is a great answer. It explains object pools and how they work very well. If you need a generic object pooling script take a look at this question and answer (yes I kind of answered my own question)
If you want to get really wild, when you need orc #21 pull up another block of orcs[20] but step carefully or you can end up with n many of these blocks with one active orc apiece in them in a worst case scenario.
Answer by Owen-Reynolds · Dec 18, 2011 at 05:43 PM
Here's a really simple example, for enemies. Spawn the max amount (20) all at once, but inactive:
// make an array of 20 enemies:
Transform[] E;
// In Start:
E = = new Transform[20];
for(int i=0;i<20;i++) {
E[i] = Instantiate(orc, Vector3.zero, Quaternion.identity) as Transform;
E[i].active = false;
}
To "spawn" one in the game, grab an inactive one. If it has lots of variables, have to reset them all. A drawback is you can't spawn more than 20
// search list for unused orc:
int orcNum = -1;
for(int i=0; i<20;i++)
if(E[i].active=false) { orcNum=i; break; }
// found one (if we couldn't find unused orc, no spawn):
if(orcNum>=0) {
// activate this orc, move it to spawn point:
E[i].active=true;
E[i].position = newOrcSpot;
// Hand-reset all orc stats: (a real instatiate would auto-do this)
E[i].GetComponent<orcScript>().hitPoints = 100;
....
}
To destroy, turn it inactive again. replace Destroy(gameObject);
with gameObject.active=false;
.
great answer, was having this problem with launching missiles that had smoke trails from a helicopter. frame rate would fall. thx again
Answer by AshwaniKumar · Jan 22, 2015 at 07:54 AM
Hello:
I have written a post on how to create an Object pool and use it in the game. Check it here.. Demo for this can be seen here.
Thanks.
Answer by SweetSodaGames · Feb 28, 2021 at 11:32 AM
Thank you for this topic. I really wanted to reach a question like this.
Because my game is sometimes spawn over 400-500 enemy in 1 second. And when starts the instantiate, fps is goes down. I created a Pool after reading this topic.
public class Pool : Singleton<Pool>
{
public GameObject[] pool;
}
And Stacked inside pool over 1000 object; sprite=null , all scripts.enabled = false.
when they die, teleport to the pool location. Theys out the map, and return to being nothing. Just a spawner script give them patrol points and who they are like "EnemyTypes.Enemy1"
My fps was droped form 60 to 23 24 without pool. Now after poolisation not even drop under 60.
Your answer
Follow this Question
Related Questions
Optimize mesh for runtime performance 0 Answers
Need optimizing tips for mobile third person shooting game. 2 Answers
Is Instantiating bullets/many objects always bad for performance? 1 Answer
Should i bake the skinned mesh? 1 Answer
Why did my render time increase after lowering the vertex count? 2 Answers