- Home /
solved it myself
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);
}
}
}
It's possible the value of attackDamage was set to 0 in the inspector, or you forgot to initialize it when you initially made this script, so it has its default value of 0 in the inspector. If you change the initializer after you add the script, you need to also change the value in the inspector since it's serialized.
Answer by jamesatighe · Jun 29, 2018 at 03:03 PM
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
Answer by Legend_Bacon · Jun 29, 2018 at 03:07 PM
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
Thanks for letting me know bout the error in the question
The debugs ive set up on this and other connect scripts are working fine except but the debugs in the void attack aren't being called
Ive already changed it to enemyHealth$$anonymous$$anager and ive had no luck.
And the attack code you included isn't being called either
The weird thing about this is that the same Attack script works when the player comes into physical contact with the enemy but not when it comes into contact with the fire it breaths
If the debugs aren’t being called then this would point to the function never being called.
Therefore I would look at the logic you are using for the if condition.
Is it possible it is never true? You have several conditions that must ALL be true order for the body of the if statement to be called.
If the playerinrange is not true or the time part is not then it won’t get called.
I would do a simple Debug.Log for each part being checked to see that they definitely have values.
James
Yeah you're right it isn't
In the attack script i have debugs telling me when it has been called and when it's in and out of range.
the attack function is only called when the player character is in physical contact with the enemy.
this is the update function that calls the attack function and the attack function (as above)
void Update () { timer += Time.deltaTime; if(enemyHealth$$anonymous$$anager != null) {
if (timer >= timeBetweenAttacks && playerInRange && enemyHealth$$anonymous$$anager.currentHealth > 0) Attack (); }
void Attack() { Debug.Log ("Attack was called from: " + gameObject.name); timer = 0f; if (playerHealth.currentHealth > 0) { Debug.Log ("Attack: damage is: " + attackDamage.ToString() + " from object: " + gameObject.name); playerHealth.TakeDamage (attackDamage); } }
can you see any problems?
Are you sure EnemyHealth$$anonymous$$anager script is attached to this fire collider in editor?
its not needed there, the enemy health manager deals with killing the enemy not the player