- Home /
The question is answered, right answer was accepted
(C#) Enemy health and take damage from bullets
I just need a clearing on what im doing. Im going to have Heath on my Enemy and its going to take damage from my bullets and when health is <= 0. So far, i have an empty script i calld EnemyHeathScript, where i put
public class EnemyHealth : MonoBehaviour {
public int EnemyMaxHealth = 100;
public int EnemyCurHealth = 100;
Thats it so far. And on my bullet script i have
public class DestroyScript : MonoBehaviour {
public float lifetime = 1.0f;
void Start ()
{
}
void Update () {
}
void Awake ()
{
Destroy (gameObject,lifetime);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
DestroyObject(other.gameObject);
Destroy(gameObject);
}
}
Not sure what to add on the bullet script and i want to know how to set up my EnemyHealthScrip so it takes damage. I know it isent that hard, but i want to be on the safe side!
Answer by dorpeleg · Mar 14, 2013 at 11:40 AM
Should be something like this:
if(other.gameObject.tag == "Enemy")
{
other.gameObject.GetComponent<name of enemy health script>().EnemyCurHealth = EnemyCurHealth - damage;
DestroyObject(other.gameObject);
Destroy(gameObject);
}
And you might want to change it so destroying the enemy only happens when EnemyCurHealth
this isn't working when we have more than one enemy, the health is decreasing from every enemy.. :/
@koslovdenis can you provide more details about how you set your enemy objects?
Follow this Question
Related Questions
Health below zero but shows negative numbers 1 Answer
Object Not Recieving Damage 3 Answers
Take health from enemy 3 Answers
One enemy triggers all the enemies 2 Answers
Problem with enemy health. 1 Answer