#asteroid object pooling thing

1 messages · Page 1 of 1 (latest)

cyan crag
mild jasper
#

working fine

#

so its all good, readding to the pool

#

working bad

#

after hitting an asteroid

#

the bullets arent going back to the pool after hitting an asteroid - this is the issue

cyan crag
#

yeah i see

#

that is very weird

mild jasper
#

this is my bullet destroyer

#

if i add some logs in the OnCollisionEnter2D, they log fine

#

so the bullet IS readding to the queue but instantly poping off somehow

#

and i am dequeuing the bullet only here

    public GameObject GetPooledBullet()
    {
        var pooledBullet = _pooledBullets.Dequeue();

        return pooledBullet;
    }
#

and i've got only 1 reference to it

cyan crag
#

so when you have something like Log "A" in the if statement and Log "B" in the AddBulletToQueue

mild jasper
#

when shooting obviously

cyan crag
#

you'll see "A" but not "B"

#

?

mild jasper
#

where?

cyan crag
#

like ```cs
public void AddBulletToQueue(GameObject bullet)
{
Debug.Log("B");
_pooledBullets.Enqueue(bullet);
}

mild jasper
#

and the A?

cyan crag
#

and then like ```cs
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.CompareTag("Asteroid"))
{
StopAllCoroutines();
Debug.Log("A");
ObjectPool.Instance.AddBulletToQueue(this.gameObject);
gameObject.SetActive(false);
}
}

#

and you'll see A but not B

#

or

#

oh wait no you said it pops off immediately

#

so it is added to the queue

#

but somehow leaves immediately?

mild jasper
#

^ this is when iim hitting the asteroid

cyan crag
#

so it does work in that sense

#

is there any other code that uses the pools

mild jasper
#

just the asteroid destroyer, bullet destroyer (readding to the queue)

#

and ship shooter

#

and asteroid generator

cyan crag
#

what's the ship shooter

mild jasper
#

basically that:

    private void Start()
    {
        if (_shootingPoint == null)
        {
            Debug.LogError($"{_shootingPoint} is null! Please asign it!", gameObject);
            return;
        }

        StartCoroutine(Shoot(_shootingInterval));
    }

    private IEnumerator Shoot(float interval)
    {
        while(true)
        {
            yield return new WaitForSeconds(interval);

            var bullet = ObjectPool.Instance.GetPooledBullet();

            if(bullet != null)
            {
                bullet.transform.position = _shootingPoint.position;
                bullet.transform.up = transform.up;
                bullet.SetActive(true);
            }
            else
            {
                Debug.LogWarning("No inactive bullet in the pool! Increase the pool amount!");
            }
        }
    }
#

shooting a bullet every 0.5f

#

works fine

#

just thatfking asteroid thing

cyan crag
#

very weird tbh

#

my pools work fine

#

and they work almost exactly the same way yours do

mild jasper
#

meh

#

i wont use queue for the bullets

#

but lists

#

mby that would help

cyan crag
#

wait i think i might know it

#

oh wait

#

hm

mild jasper
#

omg XD

#

you gonna hate me

#
    private void OnCollisionEnter2D(Collision2D other)
    {
        Destroy(gameObject);
    }
#

i had something like that in my bullet moving script

#

and i forgot about it

cyan crag
mild jasper
#

works just fine now XD

#

sorry for wasting your time