Hi,
I’m working on a script that is attached to the Camera that fades and then shortly after, destroys the object moving towards it. Unfortunately, I’m getting an error just after the object has been destroyed:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
FadingTestCam.Update ()
I’m guessing I have to remove the object from the ‘Enemies’ array ince it has been destroyed, but am not sure how. The script is shown below.
Thanks
private float Dist;
public float FadeDistance;
public GameObject[] Enemies;
void Start()
{
Enemies = GameObject.FindGameObjectsWithTag("Enemy");
}
void Update()
{
foreach (GameObject enemy in Enemies)
{
Dist = Mathf.Abs(transform.position.z - enemy.transform.position.z);
if (enemy.collider.tag == "Enemy" && Dist < FadeDistance)
{
//fade out (level of alpha 0 = transparent, time to take to fade)
iTween.FadeTo(enemy, 0.0f, 0.2f);
StartCoroutine(DestroyObject(enemy));
}
}
}
IEnumerator DestroyObject(GameObject e)
{
yield return new WaitForSeconds(1);
Destroy(e);
}