Search Unity

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

Discussion in 'Scripting' started by RaGEn, Sep 3, 2015.

  1. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    I'm trying to make an enemy throw bombs at the player and their NPC allies in my game I'm creating. Everything runs 100% fine. However I keep on getting this error "NullReferenceException: Object reference not set to an instance of an objectThrowBomb.OnTriggerEnter (UnityEngine.Collider other) (at Assets/ThrowBomb.cs:68)". This only happens when the bomb hits an allied npc which I have as prefabs (not sure if that means much).

    As I'm using this project as a way to learn more about Unity, I would like to find out why this is happening, even though it's not actually affecting my game play. If anyone has any ideas or suggestions on why this is happening I would appreciate the help. The bombs spawn from a enemy that just has a script to create the bombs then the bombs them self have this script attached to them. Here's my code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ThrowBomb : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public GameObject Explosion;
    8.     public float Life =3f;
    9.     public GameObject Soldier;
    10.     public float damage;
    11.     public bool TakeDamage;
    12.  
    13.     private HumanHealth humanhealth;
    14.     private Health health;
    15.    
    16.     void Start ()
    17.     {
    18.         float x = transform.position.x;
    19.         float z = transform.position.z;
    20.         transform.position = new Vector3(x, 1f, z);
    21.         GetComponent<Rigidbody>().velocity = transform.forward * speed;
    22.     }
    23.  
    24.     void FixedUpdate ()
    25.     {
    26.         Life -= Time.deltaTime;
    27.  
    28.         if (Life <= 0)
    29.         {
    30.             Instantiate(Explosion, gameObject.transform.position, gameObject.transform.rotation);
    31.             Destroy(gameObject);
    32.         }
    33.     }
    34.  
    35.     void OnTriggerEnter (Collider other)
    36.     {
    37.         if (other.gameObject.CompareTag ("Player")) {
    38.             Soldier = other.gameObject;
    39.             health = Soldier.GetComponent<Health> ();
    40.             Debug.Log ("Hello Player");
    41.             TakeDamage = true;
    42.             Instantiate (Explosion, gameObject.transform.position, gameObject.transform.rotation);
    43.  
    44.             if (TakeDamage == true) {
    45.                 TakeDamage = false;
    46.                 damage = 15;
    47.                 health.armor = health.armor - damage;
    48.        
    49.        
    50.                 if (health.armor <= 0) {
    51.                     health.UnitsHealth = health.UnitsHealth + health.armor;
    52.                     health.armor = 0;
    53.                 }
    54.                 Destroy (gameObject);
    55.             }
    56.         }
    57.  
    58.         if (other.gameObject.CompareTag ("Human")) {
    59.             Soldier = other.gameObject;
    60.             humanhealth = Soldier.GetComponent<HumanHealth> ();
    61.             Debug.Log ("Hello Soldier");
    62.             TakeDamage = true;
    63.             Instantiate (Explosion, gameObject.transform.position, gameObject.transform.rotation);
    64.         }
    65.         if (TakeDamage == true) {
    66.             TakeDamage = false;
    67.             damage = 15;
    68.             humanhealth.HumanArmor = humanhealth.HumanArmor - damage;
    69.  
    70.            
    71.             if (humanhealth.HumanArmor <= 0) {
    72.                 humanhealth.HumanHp = humanhealth.HumanHp + humanhealth.HumanArmor;
    73.                 humanhealth.HumanArmor = 0;
    74.             }
    75.             Destroy (gameObject);
    76.         }
    77.     }
    78. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    only object in line 68 is "humanHealth", so I'm guessing "allied npcs" don't have a "HumanHealth" script attached to them (from line 60)
     
  3. RaGEn

    RaGEn

    Joined:
    Feb 3, 2015
    Posts:
    88
    I figured it out, what you said made me think and I dumbly put an extra tag of Human on the parent of the object, so the script was trying to access that too. Thanks for your help.