Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by ItzChris92 · May 02, 2017 at 09:13 PM · scripting problemerrorerror messagegetcomponentoncollisionenter

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

I have come across a lot of people that have had this error, however I haven't been able to fix the error myself. I am simply trying to have an enemy collider deal damage to my player.

Player:

 public class PlayerHealth : MonoBehaviour {
 
     public int maxHealth = 100;
     private int currentHealth;
 
     void Awake()
     {
         currentHealth = maxHealth;
     }
 
     void Start () 
     {
         print (currentHealth);
     }
 
     void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Enemy")
         {
             currentHealth -= GetComponent<EnemyStats> ().enemyDamage;
             //currentHealth -= GetComponent<EnemyStats> ().enemyDamage;
             Debug.Log("You have been hit!" + currentHealth);
         }
     }
 }

And for the enemy:

public class EnemyStats : MonoBehaviour {

 public int maxHealth = 100; 
 private int currentHealth;
 public int enemyDamage = 10;
 public int armorRating = 10;

 void Awake()
 {
     currentHealth = maxHealth;
 }

 public void Damage(int gunDamageAmount)
 {
     currentHealth -= gunDamageAmount - armorRating;

     if (currentHealth <= 0f) 
     {
         gameObject.SetActive (false);
         Debug.Log ("Enemy killed!");
     }
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jdean300 · May 02, 2017 at 09:55 PM

The fix is always the same - when Unity pops up the error, double click on it and it will take you to the line of the error. One of the objects in that line is unitialized (null). Method calls such as GetComponent<EnemyStats>() return null when they do not find a component of the given type. You are calling GetComponent<EnemyStats>() on the gameObject that has the Player script. You probably want to get it from the enemy, which I am going to assume is the object you expect to be colliding with inside of OnCollisionEnter.

currentHealth -= collision.gameObject.GetComponent<EnemyStats>().enemyDamage;

Comment
Add comment · 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 ItzChris92 · May 02, 2017 at 10:09 PM 0
Share

Thank you so much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

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

Failed to parse configuration for gameId: xxxxxx System.Threading._ThreadPoolWaitCallback:PerformWaitCallback() 3 Answers

All compiler errors have to be fixed, but I copy pasted the code from the tutorial roll a ball to ensure its right. It still gives me the error! 0 Answers

C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer

Failed to run script updater. 1 Answer

Can't use blur 0 Answers


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