using System.Collections.Generic;
using UnityEngine;
public class BulletPool : MonoBehaviour
{
public static BulletPool bulletPoolInstance;
private List<GameObject> bulletsPooled = new List<GameObject>();
public int bulletAmountToPool;
[SerializeField] private GameObject bulletPrefab;
void Awake()
{
if (bulletPoolInstance == null)
{
bulletPoolInstance = this;
}
}
void Start()
{
for (int i = 0; i < bulletAmountToPool; i++)
{
GameObject obj = Instantiate(bulletPrefab);
obj.SetActive(false);
bulletsPooled.Add(obj);
}
}
public GameObject GetPooledBullet()
{
for (int i = 0; i < bulletsPooled.Count; i++)
{
if (!bulletsPooled[i].activeInHierarchy)
{
return bulletsPooled[i];
}
}
// Expand the pool if no bullets are available
GameObject newBullet = Instantiate(bulletPrefab);
newBullet.SetActive(false);
bulletsPooled.Add(newBullet);
return newBullet;
}
}