Object Pooling spawning error

i try to make object pooling watching the tutorial from this channel brackey

The object pooling itself works but when the object spawn it gets out of hand

Here’s the video

Here are my Pooling Object scripts which I attached this into my Object Square

using System.Collections;
    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];
            }
        }
    }

Here are my Pooling Object scripts which I attached into my gamemanager

 class PoolingObject : MonoBehaviour
    {

        public List<Pool> pools;
        public Dictionary<string, Queue<GameObject>> poolDictionary;

        #region singleton
        public static PoolingObject Instance;
        void Awake()
        {
            Instance = this;
        }
        #endregion

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

            foreach (Pool pool in pools)
            {
                Queue<GameObject> objectPool = new Queue<GameObject>();
                for (int i = 0; i < pool.size; i++)
                {
                    GameObject obj = Instantiate(pool.prefab);
                    obj.SetActive(false);
                    objectPool.Enqueue(obj);
                }

                poolDictionary.Add(pool.objectTag, objectPool);
            }
        }

        public GameObject SpawnPool(string objectTag, Vector2 posisition)
        {
            GameObject objectSpawn = poolDictionary[objectTag].Dequeue();
            objectSpawn.SetActive(true);

            objectSpawn.transform.position = posisition;

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

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

            poolDictionary[objectTag].Enqueue(objectSpawn);

            return objectSpawn;
        }
    }

Here are my Pooling Object scripts which I attached this to my spawner object

class Spawner : MonoBehaviour
    {
        PoolingObject objectPooling;

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

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

All the stuff you posted but you only put this about the actual problem. What does this mean? Maybe describe it better. A video is only good if you tell us what we’re supposed to be looking at and what you expect to happen. It’s not obvious when you look at something without knowing anything about it.

1 Like

Oh yeah I’m sorry for the mistake.

I have an endless games where it has 3 lanes Left, Middle, Right.

I want to spawn the prefab for each lane or one of them ( it will be random ) using object pooling.

Then pooling it’s self works fine, but when the object spawn the object is like the one in the video, I don’t know why the object moves like that and doesn’t stay in one of the 3 lanes.

I’m sorry for my bad English

Because you’re choosing a random position, not a random lane, when you spawn it with:

int xPosisition = Random.Range(0 - xRange, xRange + 0);

Shouldn’t be choose a random lane from circlePos?

Still same result. i think I got something to do with my spawner. but I can’t figure it out.

It would help if you replied to what I asked when I asked if you wan t to choose a random lane from “CirclePost” but you didn’t. All you said was “Still same result”. Same result as what? What did you change? I cannot read your mind!

In other words, don’t you want to do something like this?

public void OnObjectSpawn()
{
    transform.position = circlePos[Random.Range(0, circlePos.Count)];
}

I am asking you this, not telling you to do this. Please don’t answer with “it doesn’t work”.

Side note: It should be “Position” not “xPosisition”.

Yes it works I just realize that yesterday I forgot to tell you this. Thank you!

1 Like