- Home /
Question by
tommy_mc · Mar 31, 2017 at 02:41 PM ·
enemy health
Making Health Script only affect one Object
I made an enemy health script that is applied to the enemy, however I want to have multiple enemies, so when I duplicate my gameObject, the script isin't working in the sense it isin't destroying the correct object and seems to only tick down on health on one script/gameobject. So how do I make it only affect the one object that is being shot at.
public float enemyHealth;
public float damage;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void addDamage(float damage)
{
if (damage <= 0) return;
enemyHealth -= damage;
if (enemyHealth <= 0)
{
Destroy(this.gameObject);
}
}
Comment
There's nothing inherently wrong with the code you've shown -- any problems you have are in some other parts of your code. $$anonymous$$ake sure addDamage is being called on the correct object's script.