I have a script attached to my bullet prefab, which is responsible for basic movement and resetting the bullet back to the object pool after a set amount of time, I use Invoke for this.
However when the character starts shooting at first it will work just fine, but after a while bullets start disappearing while there are still other bullets in front of them (alive longer) that keep going, so my first thought is that the bullet that is being reset too early is being reset by another bullet that shouldve reset itself…
Here is the script attached to the bullets:
public class BasicMovement : MonoBehaviour
{
[SerializeField] public GameObject spellSpawn;
private Rigidbody objectRigidbody;
public float bulletRange = 2f;
public float speed;
Vector3 _localForward;
void OnEnable()
{
spellSpawn = GameObject.FindGameObjectWithTag("SpellSpawn");
objectRigidbody = transform.GetComponent<Rigidbody>();
objectRigidbody.transform.position = spellSpawn.transform.position;
objectRigidbody.velocity = transform.forward * speed;
Invoke (nameof(DestroyBullet), bulletRange);
}
void DestroyBullet()
{
gameObject.SetActive(false);
}
}
My object pooler script, here:
public static ObjectPooler SharedInstance;
void Awake()
{
SharedInstance = this;
}
void Start()
{
pooledObjects = new List<GameObject>();
for (int i = 0; i < amountToPool; i++)
{
GameObject obj = (GameObject)Instantiate(objectToPool);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
//1
for (int i = 0; i < pooledObjects.Count; i++) {
//2
if (!pooledObjects*.activeInHierarchy) {*
_ return pooledObjects*;_
_ }_
_ }_
_ //3*_
* return null;*
* }*
Thanks for the help!