- Home /
Object Pool only activating one prefab
Having an issue where I am creating a list and my spawn manager is only activating one enemy in a list of ten that are suppose to activate another random one every 5 sec see code below using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SpawnManager : MonoBehaviour
{
private static SpawnManager _instance;
public Transform SpawnPoint, Endpoint;
public GameObject[] Enemies;
public GameObject EnemyContainer;
public int EnemyCount;
public List<GameObject> objectsCreated = new List<GameObject>();
public static SpawnManager Instance
{
get
{
if (_instance == null)
{
Debug.LogError("Spawn Manager is NULL");
}
return _instance;
}
}
private void Awake()
{
_instance = this;
}
private void Start()
{
MakeEnemies();
StartCoroutine(SpawnRoute());
}
public void MakeEnemies()
{
for(int i =0; i < 10; i++)
{
var spawns = Enemies[Random.Range(0, Enemies.Length)];
Instantiate(spawns, SpawnPoint.position, Quaternion.identity,EnemyContainer.transform);
objectsCreated.Add(spawns);
spawns.SetActive(false);
new WaitForSeconds( 5);
}
}
IEnumerator SpawnRoute()
{
while (true)
{
yield return null;
foreach(var Em in objectsCreated)
{
if(Em.activeInHierarchy == false )
{
Em.SetActive(true);
Debug.Log("Called");
yield return null;
}
yield return new WaitForSeconds(5);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
public class Ai : MonoBehaviour
{
NavMeshAgent _agent;
public int _warFunds =10;
[SerializeField]
int _maxHealth;
[SerializeField]
int _minHealth;
[SerializeField]
int _currentHealth;
UI_Manager _uiManager;
// Start is called before the first frame update
void Start()
{
_agent = GetComponent<NavMeshAgent>();
_agent.SetDestination(SpawnManager.Instance.Endpoint.position);
//_agent.SetDestination(PoolManager.Instance.Endpoint.position);
_currentHealth = _maxHealth;
_uiManager = GameObject.Find("Canvas-UI").GetComponent<UI_Manager>();
}
// Update is called once per frame
//void Update()
//{
// Health();
//}
public void Die()
{
GameObject.FindObjectOfType<UI_Manager>().funds += _warFunds;
this.gameObject.SetActive(false);
}
public void Health()
{
if (_currentHealth < 1)
{
_warFunds += 10;
Die();
}
}
private void OnEnable()
{
if (_agent != null )
{
_agent.SetDestination(SpawnManager.Instance.Endpoint.position);
//_agent.SetDestination(PoolManager.Instance.Endpoint.position);
}
}
}
capture.jpg
(18.2 kB)
Comment
Best Answer
Answer by KoenigX3 · Jun 04, 2020 at 03:30 PM
How about using:
var enemyInstance = Instantiate(spawns, SpawnPoint.position, Quaternion.identity,EnemyContainer.transform);
objectsCreated.Add(enemyInstance);
enemyInstance.SetActive(false);
Your answer
Follow this Question
Related Questions
More Efficient way? 1 Answer
Array out of range 1 Answer
How to Find all objects with Tag and List their Transforms? 4 Answers
Waypoints and out of range problem.[Solved] 1 Answer
Random select from array and spawn 1 Answer