Instanced enemies not interacting correctly with player

I am trying to make a system of automatic attacks that happen when enemies are in contact with a player object, I have my enemies populate a list when they enter the players trigger and then on my players auto attack I have a for loop that (I hope) is cycling through each enemy in the list and dealing damage to them.
This all works fine if there is only one instance of my enemy prefab, when a second is added the player only begins to run its attack function when the original instanced enemy enters its trigger, however it only affects the health of the last enemy instanced.
All instances of my enemy correctly damage the player.
I am wondering what it is I am doing wrong and if anyone can help me it would be greatly appreciated as I have been stuck on this for a few days now.

I do not know where the problem is coming from so will copy all three of my scripts, thank you in advance to anyone who sifts through the whole thing

edit:- I have noticed that all of my instanced enemies are being added to the list at once, despite the intent of adding them one at a time when they collide. So I think the problem is likely coming from the way I am adding them, and the instanced scripts are not being referenced just the prefab script

My player script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Player : MonoBehaviour {

	public int attack;
	public int defense;
	public int health;
	public Text playerHealthText;
	public GameObject enemy;
	public GameObject combatManager;
	public float attackSpeed;

	private BoxCollider2D enemyTrigger;
	private CombatManager combatManagerScript;
	private bool enemyDetected;
	public float attackTimer;


	// Use this for initialization
	void Start () {

		enemy = GameObject.Find ("Enemy");
		combatManager = GameObject.Find ("CombatManager");
		playerHealthText = GameObject.Find ("PlayerHealthDisplay").GetComponent<Text> ();
		playerHealthText.text = "" + health;
		enemyTrigger = enemy.GetComponent<BoxCollider2D> ();
		combatManagerScript = combatManager.GetComponent<CombatManager> ();
		enemyDetected = false;
		attackTimer = attackSpeed;
	
	}

	void interactWithEnemy()
	{
		combatManagerScript.playerAttackAction ();
	}

	void OnTriggerStay2D (Collider2D other)
	{
		if (other == enemyTrigger)
		{
			enemyDetected = true;
		}
	}

	void OnTriggerEnter2D (Collider2D other)
	{
		if (other == enemyTrigger)
		{
			interactWithEnemy ();
		}
	}
	
	// Update is called once per frame
	void Update () {

		if (enemyDetected == true)
		{
			if (attackTimer >= 0)
			{
				attackTimer -= Time.deltaTime;
			}
			else
			{
				interactWithEnemy ();
				attackTimer = attackSpeed;
			}
		}
	}
}

My enemy script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Enemy : MonoBehaviour {

	public int attack;
	public int defense;
	public int health;
	public Text enemyHealthText;
	public GameObject combatManager;
	public GameObject player;
	public GameObject gameManager;
	// speed variable controls both the speed and direction of the moving object a positive number moves left a negative number moves right
	public float speed;
	public float attackSpeed;
	public bool playerDetected;

	public GameObject currentEnemy;
	private BoxCollider2D playerTrigger;
	private BoxCollider2D gameSpace;
	private CombatManager combatManagerScript;
	private SpriteRenderer facing;
	private float attackTimer;



	// Use this for initialization
	void Start () {

		currentEnemy = this.gameObject;
		combatManager = GameObject.Find ("CombatManager");
		player = GameObject.Find ("Player");
		gameManager = GameObject.Find ("GameManager");
		enemyHealthText = currentEnemy.GetComponentInChildren<Text>();
		enemyHealthText.text = "" + health;
		combatManagerScript = combatManager.GetComponent<CombatManager> ();
		playerTrigger = player.GetComponent<BoxCollider2D> ();
		gameSpace = gameManager.GetComponent<BoxCollider2D> ();
		facing = GetComponent<SpriteRenderer> ();
		playerDetected = false;
		attackTimer = attackSpeed;
	
	}

	void moveEnemy()
	{
		if (playerDetected == false)
		{
			// moves the attached object left across the screen
			transform.Translate (new Vector3 (-1f, 0f, 0f) * speed * Time.deltaTime);
		}
	}

	void interactWithPlayer()
	{
		combatManagerScript.enemyAttackAction ();
	}

	void OnTriggerStay2D (Collider2D other)
	{
		if (other == playerTrigger)
		{
			playerDetected = true;
		}
	}

	void OnTriggerExit2D(Collider2D other)
	{
		if (other == gameSpace)
		{
			speed = -speed;
			if (facing.flipX == true) {
				facing.flipX = false;
			}
			else
			{
				facing.flipX = true;
			}
		}
	}

	void OnTriggerEnter2D (Collider2D other)
	{
		if (other == playerTrigger) 
		{
			interactWithPlayer ();
			combatManagerScript.addToList ();
		}
	}
	
	// Update is called once per frame
	void Update () {

		moveEnemy ();
		if (playerDetected && !player)
		{
			playerDetected = false;
		}

		if (playerDetected == true)
		{
			if (attackTimer >= 0) 
			{
				attackTimer -= Time.deltaTime;
			}
			else 
			{
				interactWithPlayer ();
				attackTimer = attackSpeed;
			}
		}
	}
}

and finally my combat manager:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class CombatManager : MonoBehaviour {


	public GameObject player;
	public GameObject enemy;
	public bool playerTurn;
	public List<Enemy> enemies;

	private Player playerScript;
	private Enemy enemyScript;


	// Use this for initialization
	void Awake () {

		player = GameObject.Find ("Player");
		enemy = GameObject.FindWithTag ("Enemy");
		enemies = new List<Enemy>();
		playerScript = player.GetComponent<Player>();
		enemyScript = enemy.GetComponent<Enemy> ();
		playerTurn = true;

	}

	//the players automatic attack
	public void playerAttackAction ()
	{
		int damage = playerScript.attack - enemyScript.defense;
		int i;

		if (damage <= 0) {
			return;
		} 
		for (i = 0; i < enemies.Count; i++)
		{
			enemies_.health = enemies*.health - damage;*_

enemies_.enemyHealthText.text = “” + enemies*.health;
}
}*_

* public void enemyAttackAction ()*
* {*
* int damage = enemyScript.attack - playerScript.defense;*

* if (damage <= 0) {*
* return;*
* }*
* playerScript.health = playerScript.health - damage;*
* playerHealthUpdate ();*
* }*

* public void addToList()*
* {*
* enemies.Add (enemy.GetComponent ());*
* }*

* private void playerHealthUpdate()*
* {*
* playerScript.playerHealthText.text = “” + playerScript.health;*
* }*

* // Update is called once per frame*
* void Update () {*

* if (enemyScript.health <= 0) {*
* Destroy (enemy);*
* }*

* if (playerScript.health <= 0) {*
* Destroy (player);*
* }*
* }*
}

I found your error:

You’are adding only one enemy to the list

  • Script Combat Manager
  • Function addToList

You are ading the enemy you get at Awake to the List

  • enemy = GameObject.FindWithTag (“Enemy”);
  • That is why you are taking damage from only one Enemy

You should:

  • In your enemy script
  • When enemy collided with player
  • You add the enemy to the list
  • void OnTriggerEnter2D (Collider2D other)
    {
    if (other.gameObject.tag == “Player”){
    CombatManager.addToList(this.gameObject);

  • You need to add every enemy to the list, not Find one; Because you need to get every instance
  • @MortalTom

Good Lucky Buddy