- Home /
Damage script not changing enemy's health integer
I wrote a script to do damage to eneiemies and a quick script for the enemy's health, but the damage script isn't changing the health integer of the enemy script. So I hit an enemy and the health doesn't change.
The damage script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamagePlayer : MonoBehaviour {
public GameObject Player;
public bool NormalAttack;
public bool ChargedAttack;
void Awake() {
NormalAttack = Player.GetComponent<AttackControls>().NormalAttack;
ChargedAttack = Player.GetComponent<AttackControls>().ChargedAttack;
}
void OnTriggerEnter2D(Collider2D Enemy) {
if(Enemy.gameObject != Player) {
if(NormalAttack == true) {
Enemy.gameObject.GetComponent<HealthEnemy>().EnemyHealth-= 5;
}
else if(ChargedAttack == true){
Enemy.gameObject.GetComponent<HealthEnemy>().EnemyHealth -= 8;
}
}
}
}
The enemy's health script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthEnemy : MonoBehaviour {
public GameObject Player;
public int EnemyHealth;
void Awake () {
}
void Update () {
if(EnemyHealth == 0 || EnemyHealth < 0){
Destroy(this.gameObject);
}
}
}
Comment
Answer by Rygaran · Mar 17, 2018 at 02:28 AM
you're using OnTriggerEnter and that will only be called if the attack's collider is set to trigger, at least im assuming you're doing it that way, you should change it to OnCollisionEnter or set it to trigger in the prefab, or at awake.
Your answer
![](https://koobas.hobune.stream/wayback/20220612154738im_/https://answers.unity.com/themes/thub/images/avi.jpg)