#Object Pooling Help

1 messages · Page 1 of 1 (latest)

stiff crow
#
class Spawner : MonoBehaviour
    {
        PoolingObject objectPooling;

        void Start()
        {
            objectPooling = PoolingObject.Instance;
        }

        void FixedUpdate()
        {
            objectPooling.SpawnPool("Square", transform.position);
        }
    }

i put this script into Spawner GameObject

#
 [System.Serializable]
    public class Pool
    {
        public string objectTag;
        public GameObject prefab;
        public int size;
    }
stiff crow
#

@slow leaf This is my problem could you help me?

#

im sorry for tagging you in here.

slow leaf
#

np mate but i havent even touched pooling yet

#

i know nothing about it :/

stiff crow
#

ah it's okay, thank you for trying to help me i appreciate it.

orchid knot
#

ok so

#

I wrote my object pooling system over a year ago

#

rereading it, but it works pretty solidly

#

I use animation events to handle my pooled object deactivations, but you can do it however you want

stiff crow
orchid knot
stiff crow
#

im trying to figure it out before asking here. at first i though the error was coming from here

public class ObstacleObject : MonoBehaviour, IPooledObject
    {
        [HideInInspector]
        public int xRange = 3;
        private int currentPos = 1;

        private List<Vector2> circlePos = new List<Vector2>()
        {
            new Vector2(-4.3f, 8f),
            new Vector2(0.5f, 8f),
            new Vector2(4.3f, 8f)
        };

        void Update()
        {
            currentPos++;
            if (currentPos > 2)
            {
                currentPos = 0;
            }
        }

        public void OnObjectSpawn()
        {
            int xPosisition = Random.Range(0 - xRange, xRange + 0);
            transform.position = new Vector2(xPosisition, 6f);
        }

        void OnTriggerEnter2D(Collider2D other)
        {
            if (other.gameObject.CompareTag("whut"))
            {
                transform.position = circlePos[currentPos];
            }
        }
    }
stiff crow
orchid knot
#

that commented portion at the bottom is code you need to put in the prefab being used by the object pool

#

how you call those methods is up to you
in my case, I set PoolDestroy as public, and call it via an animation event
in the case of my after images, the animation causes them to go from 1 to 0 transparency, and then on the last frame of the animation, an event triggers PoolDestroy

#
public void LeaveAfterImage(Sprite aiSprite, Vector2 pos, Color color, bool flip, float rotation = 0)
    {
        GameObject obj = objPool.GetPooledObject();
        if (obj == null)
            return;
        SpriteRenderer aiSpriteRenderer = obj.GetComponent<SpriteRenderer>();
        aiSpriteRenderer.sprite = aiSprite;
        aiSpriteRenderer.color = color;
        aiSpriteRenderer.flipX = flip;
        obj.SetActive(true);
        obj.transform.position = pos;
        obj.transform.rotation = Quaternion.Euler(0,0, rotation);
    }``` 

here is the method that enables the prefabs for after images