Hi, this problem has been stressing me all night and any help is appreciated.
I am using c#.
I have two types of enemies, enemy1 and enemy2.
I created an enemy code and an attack code. basically it works fine for one on one situation. But if i say, use ctrl-d and make more of the same enemy, problems occur.
Firstly , i can fight many, but as soon as one dies, I cannot kill the other. (‘missing game object’ shows up). I target those with enemy1 and enemy2 tag. If either enemy dies, I cannot kill the other.
Secondly if I add many enemies, with same tags. So in this example, i spawn 3 guys, all with enemy1 tag. It seems i cannot kill them in any order I want.
I understand my problem is similar to:
http://answers.unity3d.com/questions/328022/i-need-help-to-attack-various-enemies-with-the-sam.html
but I wasnt unable to convert the javascript to c#.
From my understanding, my code only allows me to select two enemies to fight. If i make them chose from tags, they will randomly chose enemies with my selected tag.(So if i spawn 3 guys with enemy1 tags, unity chooses one of them at random to be my opponent and the other two are untouchable)
I do believe my code would need perhaps some array style fix but I am absolutely lose. Below is the attack code.
using UnityEngine;
using System.Collections;
public class atkm1 : MonoBehaviour {
public GameObject target;
public GameObject target1;
public float attackTimer;
public float coolDown;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown =0.0f;
}
// Update is called once per frame
void Update () {
GameObject go = GameObject.FindGameObjectWithTag("enemy1");
target = go;
GameObject go1 = GameObject.FindGameObjectWithTag("enemy2");
target = go1;
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(Input.GetKeyUp(KeyCode.F)) {
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
}
private void Attack() {
float distance = Vector3.Distance(target.transform.position, transform.position);
float distance1 = Vector3.Distance(target1.transform.position, transform.position);
//calculate damage for enemy1 tag
if(distance < 2.5f) {
hpP eh = (hpP)target.GetComponent("hpP");
eh.AddjustCurrentHealth(-10);
}
//calculate damage for enemy2 tag
if(distance1 < 2.5f) {
hpL eh = (hpL)target1.GetComponent("hpL");
eh.AddjustCurrentHealth(-10);
}
}
}
One last thing, I use public so i can ‘drag and drop’ the enemy objects in for testing purposes. Ofcourse even if i do that, i get the same failed results. Basically, the moment i kill one off, then i get the missing gameobject error and my player is unable to attack.
EG. target:enemy1
target1:enemy2
if i kill enemy1,
target:missing object
target1:enemy2
any help is muchly appreciated. Basically I have a prefab for an enemy that works and attack scripts that works. I wish to fight more than one enemy with same tag in any order .