Modify my script to do object pooling?

Hello, I have this script which makes pipes appear endlessly, so I have a hundred of them constantly generated. I have a script that destroys them when they come out of the screen. But I saw that it is better to recycle them than to destroy them, it would hurt performance!

How can I modify my script to do object pooling? Thank you in advance for your help.

This is my actual script :

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

public class SpwanPIPE : MonoBehaviour
{
    float next_spawn_time;
    public GameObject PIPE;
    public float height;
    void Start()
    {
        //start off with next spawn time being 'in 5 seconds'
        next_spawn_time = Time.time + 0.000001f;
    }

    void Update()
    {
        if (Time.time > next_spawn_time)
        {
            //do stuff here (like instantiate)
            GameObject newpipe = Instantiate(PIPE);
            newpipe.transform.position = transform.position + new Vector3(-10, Random.Range(-height, height), 0);

            GameObject newpipe1 = Instantiate(PIPE);
            newpipe1.transform.position = transform.position + new Vector3(10, Random.Range(-height, height), 0);


            GameObject newpipe2 = Instantiate(PIPE);
            newpipe2.transform.position = transform.position + new Vector3(30, Random.Range(-height, height), 0);


            GameObject newpipe3 = Instantiate(PIPE);
            newpipe3.transform.position = transform.position + new Vector3(50, Random.Range(-height, height), 0);


            GameObject newpipe4 = Instantiate(PIPE);
            newpipe4.transform.position = transform.position + new Vector3(70, Random.Range(-height, height), 0);

            GameObject newpipe5 = Instantiate(PIPE);
            newpipe5.transform.position = transform.position + new Vector3(90, Random.Range(-height, height), 0);

            GameObject newpipe6 = Instantiate(PIPE);
            newpipe6.transform.position = transform.position + new Vector3(110, Random.Range(-height, height), 0);

            GameObject newpipe7 = Instantiate(PIPE);
            newpipe7.transform.position = transform.position + new Vector3(130, Random.Range(-height, height), 0);

            GameObject newpipe8 = Instantiate(PIPE);
            newpipe8.transform.position = transform.position + new Vector3(150, Random.Range(-height, height), 0);

            GameObject newpipe9 = Instantiate(PIPE);
            newpipe9.transform.position = transform.position + new Vector3(170, Random.Range(-height, height), 0);

            GameObject newpipe10 = Instantiate(PIPE);
            newpipe10.transform.position = transform.position + new Vector3(190, Random.Range(-height, height), 0);

            GameObject newpipe11 = Instantiate(PIPE);
            newpipe11.transform.position = transform.position + new Vector3(210, Random.Range(-height, height), 0);

            GameObject newpipe12 = Instantiate(PIPE);
            newpipe12.transform.position = transform.position + new Vector3(230, Random.Range(-height, height), 0);


            GameObject newpipe13 = Instantiate(PIPE);
            newpipe13.transform.position = transform.position + new Vector3(250, Random.Range(-height, height), 0);


            GameObject newpipe14 = Instantiate(PIPE);
            newpipe14.transform.position = transform.position + new Vector3(270, Random.Range(-height, height), 0);






            //increment next_spawn_time
            next_spawn_time += 9991f;
        }
    }
}

This is my destroy script :

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

public class Destroy : MonoBehaviour
{
    public float dropoffPosition = -900f;

    void Update()
    {
        // Perform logic to move the object, first. That way, it trigers
        // Destroy() as soon as it oversteps its boundary.

        if (transform.position.x < dropoffPosition)
        {
            Destroy(gameObject);
        }
    }
}

Definitely work through perhaps two or three different object pooling tutorials on Youtube. The changes to your script above will be completely dependent on how the pooling is implemented. Every type of pooling has implications for object lifecycle, memory, management of offline objects, etc. Only you can judge the solutions available for suitability to your exact context.

2 Likes

The object pool itself is usually the easy part. The real work is on the object being pooled, so it can be disabled when it would otherwise be destroyed, and reset to its original state when it would otherwise be instantiated new. Most of the bugs you run into with your object pooling will likely be in your object reset code.

2 Likes

thanks.