Ai should delete a build turret

The AI builds turrets at a given position when he/she has enouph exp.

if(GM.enemy_exp >= 600)
		{
			if(BuildSpots[0].tag == "TurretSpotOpen")
			{
				Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity);
				BuildSpots[0].tag = "TurretSpotClosed";
			}
		}

Now at another state i want the AI to delete that turret, to make space for a new one how could i achieve this? I mean, how do i get the right object to destroy? How can i store the already build object to get rid of it later?

I bet it’s super simple, but sometimes i’m blind.

Thanks in advance

You could have a List of the turrets that were built, like this:

using System.Collection.Generic;

private List<GameObject> builtTurrets = new List<GameObject>();

You can make the Instantiate function return the gameobject that it created, so you can add this GO into the turret list.

gameObject newturret = Instantiate(Turrets[0], BuildSpots[0].position, Quaternion.identity) as GameObject;

builtTurrets.Add(newturret);

To destroy the turrets, you just access the references to them stored in the list. How you differentiate between turrets is up to you; Give them a unique ID in one of their scripts, change their name and add a number to it, etc. You can then, if you don’t know the turret’s index in the list, iterate through the list and check if the currently iterated turret’s ID or whatever corresponds to the one you are searching for, and then destroy that turret (don’t forget to remove it from the list first!).