Search Unity

C#: Object reference not set to an instance of an object

Discussion in 'Scripting' started by DarkBladeNemo, Dec 2, 2016.

  1. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    Hi

    So the problem I ran into today is the following. I have a script that spawns a bullet and attached to the bullet I have a script to destroy the bullet when it collides with something. So in an attempt to be able to upgrade and manage the damage of the weapons I tried making it so that when the bullet collides with the enemy it will reduce their health by the weapon damage but alas today is not my day.

    My Error:

    NullReferenceException: Object reference not set to an instance of an object
    DamageHandler.OnTriggerEnter2D () (at Assets/Scripts/DamageHandler.cs:13)

    Script it is referring to also attached to my enemies:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DamageHandler : MonoBehaviour {
    6.  
    7.     public int enemyHealth = 1;
    8.     public BulletDamageHandler damage;
    9.  
    10.     void OnTriggerEnter2D(){
    11.         Debug.Log ("Enemy Hit!");
    12.  
    13.         enemyHealth -= damage.bulletDamage;
    14.  
    15.         if(enemyHealth <= 0){
    16.             Die();
    17.         }
    18.     }
    19.  
    20.     void Die(){
    21.         Destroy(gameObject);
    22.     }
    23. }
    24.  
    The script that goes on the bullets:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletDamageHandler : MonoBehaviour {
    6.  
    7.     public int bulletHealth = 1;
    8.     public int bulletDamage;
    9.     private DamageHandler enemy;
    10.  
    11.     void OnTriggerEnter2D(Collider2D other){
    12.        
    13.         if(other.gameObject.tag == "Enemy"){
    14.             bulletHealth--;
    15.             Debug.Log ("Zombie Hit!");
    16.         }
    17.  
    18.         if(bulletHealth <= 0){
    19.             Die();
    20.         }
    21.     }
    22.  
    23.     void Die(){
    24.         Destroy(gameObject);
    25.     }
    26. }
    27.  

    So I have a feeling I am missing something by the big red error popping up but for the life of me I don't know what :/ The bullets are colliding and i have removed the part of the script that damages the enemies.. yeah I'm just gonna leave this here too tired to figure it out now.
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Double-click the error in the console and it will highlight the line of code that is causing the error; in your case:
    Code (csharp):
    1. enemyHealth -= damage.bulletDamage;
    This means you have not set the "damage" field to anything in the inspector. You need to click and drag the BulletDamageHandler onto the Damage field of your DamageHandler. Or if there are different ones for different bullets, you can change it to:
    Code (csharp):
    1. void OnTriggerEnter2D(Collider2D other){
    2.   Debug.Log ("Enemy Hit!");
    3.   var hitBy = other.GetComponent<BulletDamageHandler>();
    4.   if (hitBy) enemyHealth -= hitBy.bulletDamage;
    5. }
    That will get the bullet you've been hit by, and will ignore hits from things that aren't bullets.
     
    Xepherys likes this.
  3. DarkBladeNemo

    DarkBladeNemo

    Joined:
    Aug 23, 2013
    Posts:
    116
    Woop awesome thank you Wings you are amazing!!
     
    makeshiftwings likes this.