Greetings! I’ve had this pooling script for quite some time now, and it works perfectly. However, there’s one thing I’m not satisfied with, and that is the fact that everything is repeated. I’m not sure how I can fix this myself, so I wanted to ask here if anyone knew how I can make this shorter.
The whole script
using UnityEngine;
using System.Collections.Generic;
public class Spawner : MonoBehaviour {
public GameObject[] enemies;
public float [] startWait;
public float [] spawnTimer;
public Transform [] spawnPoints;
public Transform [] turtleSpawnPoints;
public int pooledAmountShrimp;
public int pooledAmountPuffer;
public int pooledAmountTurtle;
public int pooledAmountSwords;
public int pooledAmountBigBoy;
List<GameObject> pooledShrimp;
List<GameObject> pooledPuffer;
List<GameObject> pooledTurtle;
List<GameObject> pooledSword;
List<GameObject> pooledBigBoy;
void Start() {
pooledShrimp = new List<GameObject>();
for (int i = 0; i < pooledAmountShrimp; i++) {
GameObject obj_shrimp =
(GameObject)Instantiate(enemies[0]);
obj_shrimp.SetActive(false);
pooledShrimp.Add(obj_shrimp);
}
pooledPuffer = new List<GameObject>();
for (int i = 0; i < pooledAmountPuffer; i++) {
GameObject obj_pufferfish =
(GameObject)Instantiate(enemies[1]);
obj_pufferfish.SetActive(false);
pooledPuffer.Add(obj_pufferfish);
}
pooledTurtle = new List<GameObject>();
for (int i = 0; i < pooledAmountTurtle; i++) {
GameObject obj_turtle =
(GameObject)Instantiate(enemies[2]);
obj_turtle.SetActive(false);
pooledTurtle.Add(obj_turtle);
}
pooledSword = new List<GameObject>();
for (int i = 0; i < pooledAmountSwords; i++) {
GameObject obj_swordfish =
(GameObject)Instantiate(enemies[3]);
obj_swordfish.SetActive(false);
pooledSword.Add(obj_swordfish);
}
pooledBigBoy = new List<GameObject>();
for (int i = 0; i < pooledAmountBigBoy; i++) {
GameObject obj_bigboy =
(GameObject)Instantiate(enemies[4]);
obj_bigboy.SetActive(false);
pooledBigBoy.Add(obj_bigboy);
}
InvokeRepeating("SpawnShrimp",
startWait[0], spawnTimer[0]);
InvokeRepeating("SpawnPuffer",
startWait[1], spawnTimer[1]);
InvokeRepeating("SpawnTurtle",
startWait[2], spawnTimer[2]);
InvokeRepeating("SpawnSwords",
startWait[3], spawnTimer[3]);
InvokeRepeating("SpawnBigBoy",
startWait[4], spawnTimer[4]);
}
public GameObject GetPooledShrimp() {
for (int i = 0; i < pooledShrimp.Count; i++) {
if (!pooledShrimp[i].activeInHierarchy) {
return pooledShrimp[i];
}
}
return null;
}
public GameObject GetPooledPuffer() {
for (int i = 0; i < pooledPuffer.Count; i++) {
if (!pooledPuffer[i].activeInHierarchy) {
return pooledPuffer[i];
}
}
return null;
}
public GameObject GetPooledTurtle() {
for (int i = 0; i < pooledTurtle.Count; i++) {
if (!pooledTurtle[i].activeInHierarchy) {
return pooledTurtle[i];
}
}
return null;
}
public GameObject GetPooledSwords() {
for (int i = 0; i < pooledSword.Count; i++) {
if (!pooledSword[i].activeInHierarchy) {
return pooledSword[i];
}
}
return null;
}
public GameObject GetPooledBigBoy() {
for (int i = 0; i < pooledBigBoy.Count; i++) {
if (!pooledBigBoy[i].activeInHierarchy) {
return pooledBigBoy[i];
}
}
return null;
}
void SpawnShrimp() {
GameObject obj_shrimp = GetPooledShrimp();
if (obj_shrimp == null) return;
int spawnPointIndex = Random.Range
(0, spawnPoints.Length);
obj_shrimp.transform.position
= spawnPoints[spawnPointIndex].position;
obj_shrimp.SetActive(true);
}
void SpawnPuffer() {
GameObject obj_pufferfish = GetPooledPuffer();
if (obj_pufferfish == null) return;
int spawnPointIndex = Random.Range
(0, spawnPoints.Length);
obj_pufferfish.transform.position
= spawnPoints[spawnPointIndex].position;
obj_pufferfish.SetActive(true);
}
void SpawnTurtle() {
GameObject obj_turtle = GetPooledTurtle();
if (obj_turtle == null) return;
int spawnPointIndex = Random.Range
(0, turtleSpawnPoints.Length);
obj_turtle.transform.position
= turtleSpawnPoints[spawnPointIndex].position;
obj_turtle.SetActive(true);
}
void SpawnSwords() {
GameObject obj_swordfish = GetPooledSwords();
if (obj_swordfish == null) return;
int spawnPointIndex = Random.Range
(0, spawnPoints.Length);
obj_swordfish.transform.position
= spawnPoints[spawnPointIndex].position;
obj_swordfish.SetActive(true);
}
void SpawnBigBoy() {
GameObject obj_bigboy = GetPooledBigBoy();
if (obj_bigboy == null) return;
int spawnPointIndex = Random.Range
(0, spawnPoints.Length);
obj_bigboy.transform.position
= spawnPoints[spawnPointIndex].position;
obj_bigboy.SetActive(true);
}
}
Snippet of only one spawner
using UnityEngine;
using System.Collections.Generic;
public class Spawner : MonoBehaviour {
public GameObject[] enemies;
public float [] startWait;
public float [] spawnTimer;
public Transform [] spawnPoints;
public Transform [] turtleSpawnPoints;
public int pooledAmountShrimp;
List<GameObject> pooledShrimp;
void Start() {
pooledShrimp = new List<GameObject>();
for (int i = 0; i < pooledAmountShrimp; i++) {
GameObject obj_shrimp =
(GameObject)Instantiate(enemies[0]);
obj_shrimp.SetActive(false);
pooledShrimp.Add(obj_shrimp);
}
InvokeRepeating("SpawnShrimp",
startWait[0], spawnTimer[0]);
}
public GameObject GetPooledShrimp() {
for (int i = 0; i < pooledShrimp.Count; i++) {
if (!pooledShrimp[i].activeInHierarchy) {
return pooledShrimp[i];
}
}
return null;
}
void SpawnShrimp() {
GameObject obj_shrimp = GetPooledShrimp();
if (obj_shrimp == null) return;
int spawnPointIndex = Random.Range
(0, spawnPoints.Length);
obj_shrimp.transform.position
= spawnPoints[spawnPointIndex].position;
obj_shrimp.SetActive(true);
}
}
The snippet is basically one of five enemies that will be spawned. I figured I could make it easier for people to read, cause I’m not sure if my code is readable by others or not. So the bottom one is basically just 1/5.
Anyone know how I can optimize this? ![]()