Damage not working properly
Currently, I am trying to make a sword attack for a roguelike dungeon crawler I have been working on from a decent while now. I have a sprite for the sword and the animation of it swinging, and the beginnings of a system to make the enemies take damage on attack.
The script works like this:
There is a trigger collider around the player and a script that detects whether an enemy is within the radius using the following few lines of code:
public static float inRange;
public static List<GameObject> enemiesInRange = new List<GameObject>();
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.tag == "Enemy") {
inRange++;
enemiesInRange.Add(other.gameObject);
}
}
void OnTriggerExit2D(Collider2D other2) {
if(other2.gameObject.tag == "Enemy") {
inRange--;
enemiesInRange.Remove(other2.gameObject);
}
}
There is a function within the enemy's EnemyLogic script that makes it take damage, shown here:
public float health = 10;
// Some time later:
public void takeDamage (float amount) {
health = health - amount;
Debug.Log("TOOK DAMAGE, HEALTH NOW " + health);
}
Using the list of GameObjects created earlier, I have the following foreach loop to attempt to invoke that method on each enemy, however unsuccessfuly at this time.
foreach(GameObject enemy in SwordTrigger.enemiesInRange) {
EnemyLogic a = enemy.GetComponent<EnemyLogic>();
a.takeDamage(attackDamage);
}
As the title suggests, this is not working at this time, and it seems critical to the entire future of this game's design, I believe this is all the code attributed with the sword attack.
From the game's debug log I can determine that the enemy is registered in the inRange float, and other than that I have no idea.
Please help!
EDIT 1: I have one more insight, the issue is with the takeDamage() method, since it outputs the "TOOK DAMAGE, HEALTH NOW" but the health never goes down. I am now trying to fix that instead.
Answer by Vast342 · Jun 01 at 02:01 AM
I have figured out the solution, I forgot to set the attack damage again a while ago after recreating the script after a bug.
Setting the damage back to the intended value as 5 again makes the enemy take damage.