Disappear In Hierarchy

My ObjectPool (GameObject) From Hierarchy Get Disappeared; Please Help

Code Down Below;

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;

void Awake()
{
    PopulatePool();
}

void Start()
{
    StartCoroutine(SpawnEnemy());
}

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)
    {
        Instantiate(enemyPrefab, transform);
        EnableObjectInPool();
        yield return new WaitForSeconds(spawnTimer);
    }
}

I tried With My Code But Eventually i copied From The Github Commit

Hi Steve,

Did copying and pasting Gary’s code fix the issue for you?

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

Privacy & Terms