#Hi there I am implementing object

1 messages · Page 1 of 1 (latest)

chilly epoch
#
        {//if there is nothing available in the pool
            return Instantiate(ennemyPrefab, new Vector2(spawnX, transform.position.y), Quaternion.identity);

        }, 
        //if there is an object available
        ennemy =>
        {
            ennemy.SetActive(true);
            currentActiveEnnemies.Add(ennemy);
        },
        //we want to return the object to the pool
        ennemy =>
        {
            ennemy.SetActive(false);
            currentActiveEnnemies.Remove(ennemy);

            ennemy.transform.position = transform.position;
        },
        //if there is no more capacity in the pool
        ennemy =>
        {
            Destroy(ennemy);
        },
        false,30,100); ```
#

The aim here is simply to have a pool system to handle waves of ennemies

#

On GameOver, I simply wish to SetActive(false) on all current active gameobject of the pool

pine bridge
#

The code you've shown is only the API surface, you didn't show how ObjectPool is implemented.

chilly epoch
#

here ```using System;
using System.Collections.Generic;

namespace UnityEngine.Pool
{
//
// Résumé :
// A stack based Pool.IObjectPool_1.
public class ObjectPool<T> : IDisposable, IObjectPool<T> where T : class
{
internal readonly List<T> m_List;

    private readonly Func<T> m_CreateFunc;

    private readonly Action<T> m_ActionOnGet;

    private readonly Action<T> m_ActionOnRelease;

    private readonly Action<T> m_ActionOnDestroy;

    private readonly int m_MaxSize;

    internal bool m_CollectionCheck;

    public int CountAll { get; private set; }

    public int CountActive => CountAll - CountInactive;

    public int CountInactive => m_List.Count;

    public ObjectPool(Func<T> createFunc, Action<T> actionOnGet = null, Action<T> actionOnRelease = null, Action<T> actionOnDestroy = null, bool collectionCheck = true, int defaultCapacity = 10, int maxSize = 10000)
    {
        if (createFunc == null)
        {
            throw new ArgumentNullException("createFunc");
        }

        if (maxSize <= 0)
        {
            throw new ArgumentException("Max Size must be greater than 0", "maxSize");
        }

        m_List = new List<T>(defaultCapacity);
        m_CreateFunc = createFunc;
        m_MaxSize = maxSize;
        m_ActionOnGet = actionOnGet;
        m_ActionOnRelease = actionOnRelease;
        m_ActionOnDestroy = actionOnDestroy;
        m_CollectionCheck = collectionCheck;
    }
#
        {
            T val;
            if (m_List.Count == 0)
            {
                val = m_CreateFunc();
                CountAll++;
            }
            else
            {
                int index = m_List.Count - 1;
                val = m_List[index];
                m_List.RemoveAt(index);
            }

            m_ActionOnGet?.Invoke(val);
            return val;
        }

        public PooledObject<T> Get(out T v)
        {
            return new PooledObject<T>(v = Get(), this);
        }

        public void Release(T element)
        {
            if (m_CollectionCheck && m_List.Count > 0)
            {
                for (int i = 0; i < m_List.Count; i++)
                {
                    if (element == m_List[i])
                    {
                        throw new InvalidOperationException("Trying to release an object that has already been released to the pool.");
                    }
                }
            }

            m_ActionOnRelease?.Invoke(element);
            if (CountInactive < m_MaxSize)
            {
                m_List.Add(element);
            }
            else
            {
                m_ActionOnDestroy?.Invoke(element);
            }
        }

        public void Clear()
        {
            if (m_ActionOnDestroy != null)
            {
                foreach (T item in m_List)
                {
                    m_ActionOnDestroy(item);
                }
            }

            m_List.Clear();
            CountAll = 0;
        }

        public void Dispose()
        {
            Clear();
        }
    }
}```