- Home /
I can't have more enemies at the same time.
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.
Answer by DoTA_KAMIKADzE · Apr 05, 2015 at 02:26 PM
That is a bad idea to utilize tags in that manner. Your problem is caused by the fact that you have 2 objects with a Tag "Enemy" or more and you still use find single object with "Enemy" tag and it will just return the first one it finds.
You either need to always reference your EneList or utilize FindGameObject*s*WithTag, though if I were you I'd completely redo this nonsense and start using "Triggers and Colliders" instead.
Furthermore I'd not even use Tags but a custom enum instead, but this one is not critical and more of a personal preference.
I deleted everything and used Triggers ins$$anonymous$$d, everything works fine now. Thank you.
Your answer
Follow this Question
Related Questions
Health Bar Script Error 6 Answers
Weapon and enemy health 0 Answers
Show only selected enemy's health bar 1 Answer
I am currently working on a 3-D platformer and am running into an odd problem 0 Answers
Melee Damage script by collision 2 Answers