Enemy not giving damage

I have an enemy that breaths fire and I had a NullReferenceException.

I added ;

if(EnemyHealthManager != null)

to deal with it and while the enemy does breath fire, it does not give damage

What could the problem be?

public class MegalothAttack : MonoBehaviour
{

public float timeBetweenAttacks = 0.5f;
public int attackDamage = 25; 

GameObject player;
PlayerHealth playerHealth; 
EnemyHealthManager EnemyHealthManager;
bool playerInRange;
float timer;

void Awake ()
{
	player = GameObject.FindGameObjectWithTag ("Player");
	playerHealth = player.GetComponent <PlayerHealth> (); 
	EnemyHealthManager = GetComponent<EnemyHealthManager>();
	//anim = GetComponent <Animator> ();
}
void OnTriggerEnter(Collider col)
{
	if (col.gameObject.tag == "Player") {
		playerInRange = true;
	}
}
void OnTriggerExit(Collider col)
{
	if (col.gameObject.tag == "Player") {
		playerInRange = false;
	}
}
void Update ()
{
	timer += Time.deltaTime;
	if(EnemyHealthManager != null)  <<<ADDITION<<<
	{			
		if (timer >= timeBetweenAttacks && playerInRange && EnemyHealthManager.currentHealth > 0)  <<<this is where the null reference exception was

		Attack ();
	}
	if(playerHealth.currentHealth <= 0)
	{
		Destroy (gameObject, 2);
	}
}

void Attack ()
{
	timer = 0f; 
	if (playerHealth.currentHealth > 0) {
		playerHealth.TakeDamage (attackDamage);
	}
}

}

Not sure but I don’t think you can have the Following line.

 EnemyHealthManager EnemyHealthManager;

You have the exact same spelling for the class AND the object.

Then when you call it later

     if(EnemyHealthManager != null)  <<<ADDITION<<<
     {            
         if (timer >= timeBetweenAttacks && playerInRange && EnemyHealthManager.currentHealth > 0)  <<<this is where the null reference exception was
    }

It won’t work as you are referencing the class not the object. You can see this by the highlighted colouring.

In the if statement EnemyHealthManager is purple, showing it is referencing a class.

Rename the actual instance of the object something else (even just a different case)

 EnemyHealthManager enemyhealthmanager;

I think this will fix it.

James

Hello there,

• First you might want to rephrase your question above, it is confusing when you say “while the enemy does breath fire, it does give damage”.

• Second, in these situations always try to Debug/Print out what’s going on.

In this case, “attackDamage” might be set to zero. Or “Attack()” might not be getting called at all. To Make sure, replace your Attack() function with:

 void Attack ()
     {
         Debug.Log("ATTACK WAS CALLED: " + gameObject.name);
         timer = 0f; 
         if (playerHealth.currentHealth > 0) {
             Debug.Log("ATTACK: Damage is : " + attackDamage.ToString() + " ON OBJECT: " + gameObject.name);
             playerHealth.TakeDamage (attackDamage);
         }
     }

This may help you. If it still isn’t clear though, please post your output so we can help debug it.

• And third, your variable “EnemyHealthManager” should be “enemyHealthManager” (lowercase). Otherwise, you’re going to get errors all over the place. This might have been a copy/paste error, but I thought I would still point it out.


I hope that helps!

~Cheers,

LegendBacon