Can't destroy game object as script is still trying to access it?

Hello,

Once my player enters a box collider I want the all the game objects on the terrain to disappear. Everything does disappear as intended, except a flock of birds.
To destroy them I’m using this script - but I get the error

“Missing Reference Exception: The object of type ‘FlockController’ has been destroyed but you are still trying to access it”

This is the script I’m using to destroy the birds:

var bird1 : GameObject;
var bird2 : GameObject;

function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player")
Destroy (bird1);
Destroy (bird2):

I dragged both the bird prefab, and the flock controller script prefab from the hierarchy to the inspector, but nothing happens.

Do I need to add something to the flock controller script to stop it from trying to access the prefab after it’s been destroyed?

Thanks, Laurien

I’m assuming the game object you destroy are the same ones you created on line 31 of your FlockController script. You are storing references to the objects you Instantiate() in your _roamers array. You are accessing those entries on line 60 in your randomPositions function. The issue is that you are continuing to access objects in this array after they have been destroyed. You need to either remove the entries of the birds from the array or somehow avoid accessing them (i.e. set the entries to null for example). Or if your are deleting all the birds in the scene, remove all the entries from the array.

Add this before any code the bird destroy script with this:

var FlockControllerGameObject : GameObject;

FlockControllerGameObject.GetComponent(FlockControllerScript).KillMe();

And then add this to the Flock Controller Script at the end:

funtion KillMe()
{
Destroy (this);
}