- Home /
Enemy Health Doesnt Increase or Reset
I had a working enemy system within my game before I tried learning and adding object pooling. The health would be set to 100 and increase per wave spawned and when the player died it would be set back to 100. Not sure if that was the best way to go about this. Anyhow, now my object pool is incorporated my health no longer works as intended. If I do health = 100; OnEnable() the health wont increase per wave. If I don't set it to 100, the health will increase but the old way of setting it to 100 on death doesn't work. Any help would be appreciated.
There's a lot of code so ill try and condense it to strictly just the code involving the health.
public class EnemyBehaviour : $$anonymous$$onoBehaviour{
public float currentHealth;
private void OnEnable()
{
currentHealth = 100;
}
void OnTriggerEnter2D(Collider2D collision){
if ( collision.tag == ("bullet")) TakeDamage(powerFactor);
void TakeDamage(float powerFactor){
currentHealth -= bulletDamage * powerFactor;
if(currentHealth = 0) GameOver();
}
public class GameController : $$anonymous$$onoBehaviour {
public float enemyHealth$$anonymous$$ultiplier;
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject enemy = ObjectPooler.SharedInstance.GetPooledObject("Enemy");
enemy.GetComponent<EnemyBehaviour>().currentHealth *= enemyHealth$$anonymous$$ultiplier;
if (enemy != null)
{
enemy.transform.position = spawnPosition1;
enemy.transform.rotation = spawnRotation1;
enemy.SetActive(true);
}
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
}
}
}
Answer by hameed-ullah-jan · Apr 08, 2019 at 02:42 PM
can you share your code, because your statement is not clear enough to get the whole info
I updated my code to involve everything to do with my currentHealth as I have it. However, the extra is more to do with taking away health than making it increase
Answer by Tsaras · Apr 08, 2019 at 05:38 PM
The problem is in this segment:
for (int i = 0; i < hazardCount; i++)
{
GameObject enemy = ObjectPooler.SharedInstance.GetPooledObject("Enemy");
enemy.GetComponent<EnemyBehaviour>().currentHealth *= enemyHealthMultiplier;
if (enemy != null)
{
enemy.transform.position = spawnPosition1;
enemy.transform.rotation = spawnRotation1;
enemy.SetActive(true);
}
yield return new WaitForSeconds(spawnWait);
}
You are setting the enemy's health before you activate it. So OnEnable() is called after enemy.GetComponent<EnemyBehaviour>().currentHealth *= enemyHealthMultiplier;
therefore resetting the health to 100.