Enemies not enabling

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.");
    }
}

}

The error seems to be indicating the problem is a coroutine in your Enemy script, not the ObjectPool script. The project in the lectures has an EnemyHealth and EnemyMover script so it’s difficult to know what’s in your Enemy script which may be the source of the error.

Thank you for responding!

i would like to say thats my Issue is fixed!

1 Like

I’m glad you were able to fix it!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms