2D game feel course object pooling bullet error

When firing I sometimes get an error with object pooling trying to access an already deleted bullet. My code should be exactly the same as in the lecture. This will sometimes cause the bullets to not always travel the full distance. Does anyone know what I should tweak to stop this error?

1 Like

can you send the code where you are trying to use Bullet?

This is the code I currently have.

using System;

using System.Collections;

using System.Collections.Generic;

using Cinemachine;

using UnityEngine;

using UnityEngine.Pool;

public class Gun : MonoBehaviour

{

public static Action OnShoot;

[SerializeField] private Transform _bulletSpawnPoint;

[SerializeField] private Bullet _bulletPrefab;

[SerializeField] private float _gunFireCD = .5f;

private ObjectPool<Bullet> _bulletPool;

private static readonly int FIRE_HASH = Animator.StringToHash("Fire");

private Vector2 _mousePos;

private float _lastFireTime = 0f;

private CinemachineImpulseSource _impulseSource;

private Animator _animator;

private void Awake()

{

    _impulseSource = GetComponent<CinemachineImpulseSource>();

    _animator = GetComponent<Animator>();

}

private void Start()

{

    CreateBulletPool();

}



private void Update()

{

    Shoot();

    RotateGun();

}

private void OnEnable()

{

    OnShoot += ShootProjectile;

    OnShoot += ResetLastFireTime;

    OnShoot += FireAnimation;

    OnShoot += GunScreenShake;

}

private void OnDisable()

{

    OnShoot -= ShootProjectile;

    OnShoot -= ResetLastFireTime;

    OnShoot -= FireAnimation;

    OnShoot -= GunScreenShake;

}

public void ReleaseBulletFromPool(Bullet bullet)

{

    _bulletPool.Release(bullet);

}

private void CreateBulletPool()

{

    _bulletPool = new ObjectPool<Bullet>(() => {

        return Instantiate(_bulletPrefab);

    }, bullet => {

        bullet.gameObject.SetActive(true);

    }, bullet => {

        bullet.gameObject.SetActive(false);

    }, bullet => {

        Destroy(bullet);

    }, false, 20, 40);

}

private void Shoot()

{

    if (Input.GetMouseButton(0) && Time.time >= _lastFireTime) {

       OnShoot?.Invoke(); // ? null conditional operator to fire only when subscribed to avoid null exception

    }

}

private void ShootProjectile()

{

    Bullet newBullet = _bulletPool.Get();

    newBullet.Init(this, _bulletSpawnPoint.position, _mousePos);

}

private void FireAnimation()

{

    _animator.Play(FIRE_HASH, 0, 0f);

}

private void ResetLastFireTime()

{

    _lastFireTime = Time.time + _gunFireCD;

}

private void GunScreenShake()

{

    _impulseSource.GenerateImpulse();

}

private void RotateGun()

{

    _mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

    Vector2 direction = PlayerController.Instance.transform.InverseTransformPoint(_mousePos);

    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;

    transform.localRotation = Quaternion.Euler(0, 0, angle);

}

}

Don’t Destroy(bullet). This will only destroy the component, not the actual bullet, leaving a gameObject that wants to be a bullet but has no Bullet functionality. What you want is Destroy(bullet.gameObject)

1 Like

It happens much less often but the same error still seems to pop up every now and then but I’m willing to settle for this.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms