I am currently building a wave based survival game, with the enemies using the Invector enemyAI and melee controller but each enemy spawned would cause more than a 10 fps drop. I added the Lean Pool object pool script from the asset store but the fps still drops like crazy each time a new enemy is spawned.
I also noticed the enemies weren’t being returned to the pool. Their bodies remained in the Hierarchy upon death. Can anyone help me out here?
You need to use
Destroy(GameObject);
for destroying the bodies from the scene and the Hierarchy.
And for the pool, you should have some code chechik wich enemies are alive and which enemies are again/still in the pool
instantiatiation and destroying are costly process. you could try below code
public List<GameObject> enmList = new List<GameObject> ();
public GameObject EnemyPrefab;
void Start () {
for (int i = 0; i < 10; i++) {
GameObject em = Instantiate (EnemyPrefab, transform.position, Quaternion.identity);
em.SetActive (false);
enmList.Add (em);
}
}
// call this method where you want enemy to spawn.
GameObject GetEnemy(){
for (int i = 0; i < enmList.Count; i++) {
if (!enmList *.activeInHierarchy) {*
_ enmList .transform.position = // change position according to your needs_
_ enmList .SetActive (true);
return enmList ;
* }
}
GameObject em = Instantiate (EnemyPrefab);
em.transform.position = // change position according to your needs*
* em.SetActive (true);
enmList.Add (em);
return em;
}*
Note:
don’t destroy the enemy. make set active false;_