I have this script here that will award gold to the player if the game object(AIPlayer) “dies” (if his health <= 0) And also it will destroy the game object, however there are 4 enemies in the scene, instantiated at runtime from a AIPlayer prefab. When one enemy reaches 0 health, all 4 are destroyed and the player is awarded the proper amount of gold for each kill. How can I set it to only destroy a specific object instead of the clones of the prefab that is currently destroying.

void Awake () 
{
	currency = GameObject.FindGameObjectWithTag("GameManager").GetComponent<Currency>();
	player = GameObject.FindGameObjectWithTag("AIPlayer").GetComponent<AIPlayer>();
}

void Update () 
{
	if(player.HP <= 0)
	{
		Destroy(this.gameObject);
		currency.CalculateGold(25);
	}
	else if(player.HP > 0)
	{

	}

Does it work if you change this line:

player = GameObject.FindGameObjectWithTag("AIPlayer").GetComponent<AIPlayer>();

to

player = this.gameObject.GetComponent<AIPlayer>();

Because this script is on every enemy, I believe it should be able to find the component of itself rather than finding the component using the tags.

The problem is that FindGameObjectWithTag will look for an object in all your scene, so your enemies may end referencing other enemies through the ‘player’ variable. Since both scripts (AIPlayer and this one) are attached to the same GameObject, you only need to use a GetComponent to find the specific AIPlayer for the object.

player = GetComponent<AIPlayer>();