Make projectile go to the object pooler when it collides

Hi, I have this code right here for an object pooler I saw in a Brackeys video OBJECT POOLING in Unity - YouTube (I edited it to have a function for projectiles too)


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPooler : MonoBehaviour
{
    [System.Serializable]
    public class Pool
    {
        public string tag; //The name of the object
        public GameObject prefab; //The object
        public int size; //The size of the pool
    }

    public static ObjectPooler Instance;

    private void Awake()
    {
        Instance = this;
    }

    public List<Pool> pools; //The pools it has
    public Dictionary<string, Queue<GameObject>> poolDictionary; //The dictionary of pools

    void Start()
    {
        poolDictionary = new Dictionary<string, Queue<GameObject>>();

        foreach (Pool pool in pools)
        {
            Queue<GameObject> objectPool = new Queue<GameObject>(); //Create a queue

            for (int i = 0; i < pool.size; i++) //Adds the objects to the queue
            {
                GameObject obj = Instantiate(pool.prefab);
                obj.SetActive(false);
                objectPool.Enqueue(obj);
            }
            poolDictionary.Add(pool.tag, objectPool);
        }
    }
    public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation)
    {
        if (!poolDictionary.ContainsKey(tag))
        {
            Debug.LogWarning("Pool with tag " + tag + "doesn't exist");
            return null;
        }
        GameObject objectToSpawn = poolDictionary[tag].Dequeue();

        objectToSpawn.SetActive(true);
        objectToSpawn.transform.position = position;
        objectToSpawn.transform.rotation = rotation;

        IPooledObject pooledObj = objectToSpawn.GetComponent<IPooledObject>();

        if (pooledObj != null)
        {
            pooledObj.OnObjectSpawn();
        }

        poolDictionary[tag].Enqueue(objectToSpawn);

        return objectToSpawn;
    }
    public GameObject SpawnFromPoolWithSpeed(string tag, Vector3 position, Quaternion rotation, Vector3 speed)
    {
        if (!poolDictionary.ContainsKey(tag))
        {
            Debug.LogWarning("Pool with tag " + tag + "doesn't exist");
            return null;
        }
        GameObject objectToSpawn = poolDictionary[tag].Dequeue();

        objectToSpawn.GetComponent<MovingProjectile>().movement = speed;
        objectToSpawn.SetActive(true);
        objectToSpawn.transform.position = position;
        objectToSpawn.transform.rotation = rotation;

        IPooledObject pooledObj = objectToSpawn.GetComponent<IPooledObject>();

        if(pooledObj != null)
        {
            pooledObj.OnObjectSpawn();
        }

        poolDictionary[tag].Enqueue(objectToSpawn);

        return objectToSpawn;
    }
}

And here’s the code for the projectile

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerProjectile : MonoBehaviour

{

public int damage;
    void OnCollisionEnter2D(Collision2D col)
    {
        if(col.gameObject.tag == "Enemy")
        {
            col.gameObject.GetComponent<EnemyScript>().TakeDamage(damage);
        }
        Destroy(gameObject);
    }
}

What I wanna do is that when the projectile collides

@EneaForEnea , when an object is spawned, it is removed from the queue (dequeue), and set to active. When you are done using it, enqueue it and set it to inactive. You can see examples of those calls in the code .

I think that’s how it should work, anyway, but there are a couple of suspicious enqueue calls at the end of each spawn function that I don’t understand - I’d have to see the IPooledObject script. But if it works, go with it!