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?

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.

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 *= Instantiate(orc, Vector3.zero, Quaternion.identity) as Transform;*

E*.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*.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*.active=true;*
E*.position = newOrcSpot;*
// Hand-reset all orc stats: (a real instatiate would auto-do this)
E*.GetComponent().hitPoints = 100;*

}
To destroy, turn it inactive again. replace Destroy(gameObject); with gameObject.active=false;.

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.

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.