Object pooling with different type of enemies problem. C#
Hello guys, It's the first time I do object pooling so i'm trying to learn the basics. Until now i always instantiated and destroyed enemies when they spawned/died. I read that doing this it's not well optimized and it's better using object pooling. Enemies data are ScriptableObjects, and i've got a level (scriptableObject too) that has got every enemy that should spawn inside that level. This is what i have done until now:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemySpawnScript : MonoBehaviour {
#pragma warning disable 0649
private GameObject enemy;
#pragma warning restore 0649
public void enemySpawn (LevelData level){
//here I loop for every ScriptableObject inside the level to instantiate the right prefabs.
foreach (EnemyDataSpawn enemyDataSpawn in level.dataSpawner) {
StartCoroutine (InstantiateObject (enemy, enemyDataSpawn));
}
}
IEnumerator InstantiateObject (GameObject enemy, EnemyDataSpawn enemyDataSpawn){
LevelManager.previousEnemyDelay += enemyDataSpawn.delay;
yield return new WaitForSeconds(LevelManager.previousEnemyDelay);
enemy = Instantiate(Resources.Load("EnemyPrefabs/"+enemyDataSpawn.enemy.enemyID)) as GameObject;
enemy.name = enemyDataSpawn.enemy.enemyID;
enemy.transform.position = enemyDataSpawn.spawnPoints;
EnemyScript.InitializeEnemyData(enemyDataSpawn);
}
}
I do "LevelManager.previousEnemyDelay += enemyDataSpawn.delay" because I want that every enemy spawns with a delay (set inside the enemy ScriptableObject that now i call enemyDataSpawn) that begin when the previous enemy has spawned. Now i've studied a little about object pooling and I was able to use it for the player bullets following the unity video tutorial about pooling. The question is: how can I do object pooling with different types of enemy that should instantiate in a precise order (that can be modified in the future, so i cannot know how many enemies, what kind of enemies will spawn, etc), exactly like the code above(but using the pooling technique, without instantiating and destroying everytime)? Thank you in advance.
Answer by kdpkke · Feb 11, 2017 at 08:24 PM
In the end I found this tutorial and it works very well. It is well made and expands the unity lesson about object pooling. I suggest everyone to take a look at this!
Your answer
Follow this Question
Related Questions
Could I get an explanation of object pooling and how to do it? 2 Answers
Trouble with object pooling 2 Answers
object pooler with delayed object pools 0 Answers