• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 02, 2018 at 04:37 PM by Nosmo for the following reason:

solved it myself

avatar image
Question by Nosmo · Jun 29, 2018 at 02:43 PM · enemynullreferenceexceptionnullenemy damagehealth

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);
     }
 }

}

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Vicarian · Jun 29, 2018 at 02:47 PM 0
Share

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.

2 Replies

  • Sort: 
avatar image

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

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nosmo · Jun 29, 2018 at 03:29 PM 0
Share

I had a few errors but when I fixed them it didn't work :(

avatar image

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

Comment

People who like this

0 Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nosmo · Jun 29, 2018 at 03:46 PM 0
Share

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 enemyHealthManager 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

avatar image jamesatighe Nosmo · Jun 30, 2018 at 05:46 PM 0
Share

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

avatar image Nosmo jamesatighe · Jul 02, 2018 at 01:11 PM 0
Share

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(enemyHealthManager != null) {
if (timer >= timeBetweenAttacks && playerInRange && enemyHealthManager.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?

Show more comments
avatar image Ermiq Nosmo · Jun 30, 2018 at 08:44 PM 0
Share

Are you sure EnemyHealthManager script is attached to this fire collider in editor?

avatar image Nosmo Ermiq · Jul 02, 2018 at 12:02 PM 0
Share

its not needed there, the enemy health manager deals with killing the enemy not the player

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

93 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Health and ApplyDamage to tagged objects - Help 1 Answer

Apply Damage To Player On Collison With Specific Game Object 1 Answer

Enemy atributes script 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

how to fix a null reference exception when a findgameobjectwithtag does not exist 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges