• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Get Component Script Error 1 Answer

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

Create a copy of a non MonoBehaviour class and set variables from other class 0 Answers

error CS0120: An object reference is required to access non-static member `TextManagerLvl1.currentState' 1 Answer

Can't use blur 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges