using System.Collections.Generic;
using UnityEngine;
public class NPCPool : MonoBehaviour
{
public static NPCPool npcInstance; // Singleton for global access
public GameObject npcPrefab;
public int poolSize = 5;
private Queue<GameObject> pool = new Queue<GameObject>();
private void Awake()
{
// Assign singleton reference
npcInstance = this;
// Pre-instantiate NPCs and add them to the pool
for (int i = 0; i < poolSize; i++)
{
GameObject npc = Instantiate(npcPrefab);
npc.SetActive(false); // Keep inactive until needed
pool.Enqueue(npc);
}
}
// Retrieve an NPC from the pool
public GameObject GetFromPool()
{
if (pool.Count > 0)
{
GameObject npc = pool.Dequeue();
npc.SetActive(true);
return npc;
}
Debug.LogWarning("No NPCs available in the pool.");
return null;
}
// Return an NPC to the pool
public void ReturnToPool(GameObject npc)
{
npc.SetActive(false);
pool.Enqueue(npc);
}
}