- Home /
Enemies always dies in sequence
i have this problem where the enemy spawned first always die first.
Say i spawned 10 enemies, the first one spawned always die first, then the second, then the third and so on even though i only attack the last one.
Here's the Enemy Health Script.
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour
{
public int enemyHealth;
public GameObject particle;
public GameObject money;
void Update()
{
if (enemyHealth <= 0)
{
Destroy(gameObject);
}
}
}
Here's the Damage Script, this is attached to the projectiles
using UnityEngine;
using System.Collections;
public class ProjectileDamage : MonoBehaviour
{
public int weaponDamage;
private GameObject enemy;
private EnemyHealth enemyHp;
void Awake()
{
enemy = GameObject.FindGameObjectWithTag("Enemy");
enemyHp = enemy.GetComponent<EnemyHealth>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
enemyHp.enemyHealth -= weaponDamage;
Destroy(gameObject);
}
if (other.tag == "wall")
{
Destroy(gameObject);
}
}
}
Answer by Suddoha · Sep 10, 2015 at 07:09 PM
Apparently that's due to the fact you're using FindGameObjectWithTag. The method doesn't know which one of the enemies you want to have a reference to, it will return the first which is found.
Instead of looking for the enemie in the Awake() method, use the coliider variable 'other' in order to access the enemy that you've hit.
This should look something like:
public class ProjectileDamage : MonoBehaviour
{
public int weaponDamage;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
EnemyHealth enemyHp = other.GetComponent<EnemyHealth>();
if (enemyHp != null)
enemyHp.enemyHealth -= weaponDamage;
Destroy(gameObject);
}
else if (other.CompareTag("wall"))
{
Destroy(gameObject);
}
}
}
Make sure you use if - else if to ensure you stop comparing tags as soon as one is found. Also, your tags sometimes start with a uppercase letter, then with a lowercase. This can get pretty confusing and messy.
In order to avoid typos and unmaintable code, it's recommended to make a static class with public const string values that represent your tags. You can then use these variables instead of your tags, as soon as a tag changes, simply adjust it once in that class and it will automatically be used everywhere else where the tag variable is used.
Answer by tyoc213 · Sep 11, 2015 at 04:36 PM
If you see in the headachy you have a lot of enemies tagged Enemy.... but this function only return the first, so that is why, also note that there is one plural function for that which will return the current list.
enemy = GameObject.FindGameObjectWithTag("Enemy");
best use the collider on the enemies and attach there the on hit thing, so you will always have "automatically" your actual enemy and not the first one on the list.
(NOTE: as a test, you can change enemy colors and reorder them, see who die first if the first spawned or the first in the hierarchy).
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i make a ray cast take health from enemies 2 Answers
FallDamage being given continously instead of just once 1 Answer
Programming an enemy A.I on Unity 0 Answers