Hey Unity people. when i have one enemy in my scene, both my damage- and healthscript works fine, but when i duplicate my enemy i cant do damage to the original object.
Here is my PlayerAttackScript :
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public int Damage = 10; //The Players BaseDamage
public int MeleeDamage = 10; //The Players MeleeDamage
public int RangedDamage = 15; //The Players RangeDamage
private float PlayerDistance; //The Players Distance
private Transform myTransform; //The Players transform
private Transform target; //The Enemyes transform
public List<GameObject> EneList; //The Enemies List
public GameObject prefab;
public Transform selectTarget;
void Awake(){
myTransform = transform; //Sets MyTransform = Players transform
PlayerDistance = 2; //The Distance of the players attack
}
void Start(){
EneList = new List<GameObject>();
selectTarget = null;
AddAllEnemies();
}
public void AddAllEnemies(){
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.gameObject);
}
public void AddTarget(GameObject enemy){
EneList.Add(enemy);
}
// Update is called once per frame
void Update () {
GameObject ThePlayer = GameObject.FindWithTag("Player");
PlayerStamina PlayerStam = ThePlayer.GetComponent<PlayerStamina>();
if(Input.GetKeyDown(KeyCode.R)){ //If R is pressed
if(PlayerStam.StaminaE == true){ //If player has enought Stamina
if(PlayerDistance <= 2){ //Checks that the distance is smaller than the MaxDistance
gameObject.SendMessage("StaminaHit", 20.0f);
GameObject.FindWithTag("Enemy").SendMessage("DamageEnemy", 20.0f);
}
}
}
GameObject go = GameObject.FindGameObjectWithTag("Enemy"); //sets the var target to a transform
target = go.transform; //Makes the target = the Enemys transform
PlayerDistance = Vector3.Distance(target.position, myTransform.position); //Checks the Enemys Distance from Player
}
}
And this is my EnemyHealthScript :
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int EneHealth = 200;
public int EneCurHealth = 200;
void DamageEnemy (int Damage){
GameObject TheEnemy = GameObject.FindWithTag("Enemy");
EnemyAI EnemyAI = TheEnemy.GetComponent<EnemyAI>();
if(EnemyAI.distance <= 2){
EneCurHealth -= Damage;
}
}
// Update is called once per frame
private void Update () {
if(EneCurHealth <= 0){
EneCurHealth = 0;
}
if(EneCurHealth == 0){
Destroy(gameObject);
}
}
}
Thank you in Advance.