AI Script Errror Crash After Destruction

i am working on a first person shooting game and have recently finished shooting and damage scripts which has been a succsess however everytime i destroy a enemy the game goes black abruptly followed by this error meesage: missing efrense Exception: the object type ‘Transorm’ has been destroyed but you are stilly trying to accssess it.

here is the codeing block that this is linked too found in the enemy AI Script.

I actually had the exact same problem when I was working on my targetting script. What I ended up doing is making a check to the enemy health to make sure that if the health of the enemy was equal to 0, that I set the gameobject.active = false. After setting the gameobject.active = false, any time I would destroy a gameobject I would go back and get a list of new enemies that are on the current screen. See example:

public void TargetAllEnemies()
{
	//Make an array of gameobjects all with the tag of enemy
	GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
	targets.Clear();
	
	foreach(GameObject enemy in go)
	{
		targets.Add(enemy.transform);			
	}
}

private void TargetEnemy()
	{
		if(selectedTarget == null)
		{
			SortTargetsByDistance();
			selectedTarget = targets[0];
		}
		else
		{
			if(!selectedTarget.gameObject.active)
			{	
				Destroy(selectedTarget.gameObject,0);
				TargetAllEnemies();
				SortTargetsByDistance();
				selectedTarget = targets[0];
			}
[.....]

The problem really exists that if you store an array of gameobjects and you destroy one of them that is supposed to exist in the array, you are trying to reference an object that no longer exists. Therefore, once the object is destroyed, you need to alter the array by grabbing a new set of gameobjects.

Just check if target exists before you access any properties of target.

if (!target) return false;

to silicon how would i got about implementing the script into my exsisting script?