Hello there! my Ram’s are Instantiating but they are still Disabled, i also get an error when i run - Coroutine couldn’t be started because the the game object ‘Ram(Clone)’ is inactive!
UnityEngine.MonoBehaviour:StartCoroutine (System.Collections.IEnumerator)
Enemy:OnEnable () (at Assets/Scripts/Enemy.cs:14)
ObjectPool:EnableObjectInPool () (at Assets/Scripts/ObjectPool.cs:41)
ObjectPool/d__8:MoveNext () (at Assets/Scripts/ObjectPool.cs:51)
UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)
i tried Enabling them manually but whenever i click on the enable button. the check mark doesn’t show on the box, can anyone help?
my ObjectPool script: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoBehaviour
{
[SerializeField] GameObject enemyPrefab;
[SerializeField] int poolSize = 5;
[SerializeField] float spawnTimer = 1f;
GameObject[] pool; // array named pool
void Awake()
{
PopulatePool();
}
void Start()
{
StartCoroutine(SpawnEnemy());
Debug.Log("Coroutine works.");
}
void PopulatePool()
{
pool = new GameObject[poolSize];
for(int i = 0; i < pool.Length; i++)
{
pool[i] = Instantiate(enemyPrefab, transform);
pool[i].SetActive(false);
}
}
void EnableObjectInPool()
{
for(int i = 0; i < pool.Length; i++)
{
if(pool[i].activeInHierarchy == false)
{
pool[i].SetActive(true);
return;
}
}
}
IEnumerator SpawnEnemy()
{
while (true)
{
EnableObjectInPool();
yield return new WaitForSeconds(spawnTimer);
Debug.Log("ObjectPoolScript Coroutine is running.");
}
}
}