#asteroid object pooling thing
1 messages · Page 1 of 1 (latest)
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
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
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
so when you have something like Log "A" in the if statement and Log "B" in the AddBulletToQueue
when shooting obviously
where?
like ```cs
public void AddBulletToQueue(GameObject bullet)
{
Debug.Log("B");
_pooledBullets.Enqueue(bullet);
}
and the A?
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?
just the asteroid destroyer, bullet destroyer (readding to the queue)
and ship shooter
and asteroid generator
what's the ship shooter
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
very weird tbh
my pools work fine
and they work almost exactly the same way yours do
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