Enemy kill counter only counts first killed enemy
Hi, unity beginner here. I'm trying to add a enemy kill count to my game. At the moment I have 4 different types of enemies that all share the same "EnemyHealth" script, and the counter works... but, only for the first enemy killed. The counter stays on 1 for every next kill, and I've no idea why. The debug shows that every kill is registering. All the enemies are prefabs(don't know if that is of importance).
would be nice if anyone could help, trying to fix this for 4 days now.
this is the Enemy Health script. Thanks in advance :)
public class EnemyHealth : MonoBehaviour {
int maxHealth = 5;
public int currentHealth;
Animator enemyAnim;
Rigidbody2D enemyRb;
public int currentHeadCount;
[SerializeField] Text headCountText;
public bool isDead;
void Start()
{
enemyRb = GetComponent<Rigidbody2D>();
enemyAnim = GetComponent<Animator>();
currentHealth = maxHealth;
currentHeadCount = 0;
UpdateHeadCount();
}
void Update()
{
if (currentHealth <= 0)
{
if (!isDead)
{
EnemyDeath();
AddToCounter();
Debug.Log("Counter added");
isDead = true;
}
}
}
public void EnemyTakeDamage(int damage)
{
if (currentHealth > 0)
{
currentHealth -= damage;
}
}
void EnemyDeath()
{
enemyAnim.SetBool("IsDead", true);
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Bullet"))
{
EnemyTakeDamage(1);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Grenade"))
{
EnemyTakeDamage(10);
}
}
public void UpdateHeadCount()
{
headCountText.text = currentHeadCount.ToString();
}
public void AddToCounter()
{
currentHeadCount++;
UpdateHeadCount();
}
}
Your answer
Follow this Question
Related Questions
Rotating Movement with Acceleration problem 1 Answer
How can i access another scripts GameObject Array to check its inside if there are any tower 1 Answer
I have no Idea why my highscore Script isn't working ... 0 Answers
Open door by completing all puzzles 1 Answer
Problem with layer mask and nodes 1 Answer