Why i can't acces a global list in my class from OnDestroy() function
Hi there, well i got a problem since 1 hour now and i really can't figure out what it is, here's my code, i explain myself, this code is simply spawning a projectile in my game when a player is clicking, when it's happening, i instantiate my projectile gameobject in a list to keep track of all the spawned projectiles, set a timed destroy to it to avoid lag, and i wan't to remove the unused element in my list associated with the projectile, the thing is, when i Debug.log() the lenght of my list in the Update() method, the lenght infinitely increment as i make projectile spawn, but when i Debug.log() it in the OnDestroy() method, the lenght is always zero, The OnDestroy() method seems to not be able to see my List that is incrementing in the Update() method and i can't figure out why
(I hope my english was well, not my main langage i'm sorry)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Projectile : MonoBehaviour
{
[SerializeField] public GameObject projectile;
protected Rigidbody rb;
protected float timeToLive = 3f;
protected Vector3 launcher;
protected Vector3 target;
protected float power;
protected bool shoot = false;
public List<GameObject> tempProjectile;
public void Shoot(Vector3 _launcher, Vector3 _target, float _power)
{
launcher = _launcher;
target = _target;
power = _power;
shoot = true;
}
private void Update()
{
if (shoot)
{
tempProjectile.Add(Instantiate(projectile, launcher, Quaternion.LookRotation(launcher - target)));
rb = tempProjectile[tempProjectile.Count-1].GetComponent<Rigidbody>();
rb.AddForce((-(launcher - target + new Vector3(0, 0, 0)).normalized) * power, ForceMode.VelocityChange);
Destroy(tempProjectile[tempProjectile.Count-1], timeToLive);
shoot = false;
}
Debug.Log(tempProjectile.Count);
}
private void OnDestroy()
{
tempProjectile.RemoveAt(0);
}
private void OnTriggerEnter(Collider other)
{
//Debug.Log(other.name);
}
}