Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

NullReferenceException help

Discussion in 'Scripting' started by Epikmemer45, Jan 9, 2021.

  1. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    I'm trying to add damage when an enemy touches you in my game, but it keeps putting this NullReferenceException error. The actual damage and health bar stuff works the way I want it to without anything wrong actually happening in the game, but the error still appears in my console. Here is my code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Enemy : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public float speed = 4f;
    8.     public LayerMask whatIsPlayer;
    9.     public float sightRange;
    10.     public float health = 50f;
    11.     public int ouchies = 20;
    12.     public bool playerInSightRange;
    13.     Rigidbody rig;
    14.  
    15.     private void Start()
    16.     {
    17.         target = GameObject.Find("Sourkid").transform;
    18.         rig = GetComponent<Rigidbody>();
    19.     }
    20.  
    21.     private void Update()
    22.     {
    23.         playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
    24.         if (playerInSightRange) Attack();
    25.     }
    26.  
    27.     private void Attack()
    28.     {
    29.         Vector3 pos = Vector3.MoveTowards(transform.position, target.position, speed * Time.fixedDeltaTime);
    30.         rig.MovePosition(pos);
    31.         transform.LookAt(target);
    32.     }
    33.  
    34.     private void OnTriggerEnter(Collider other)
    35.     {
    36.  
    37.        other.gameObject.GetComponent<helth>().getHurt(ouchies);
    38.     }
    39.  
    40.     public void TakeDamage (float amount)
    41.     {
    42.         health -= amount;
    43.         if (health <= 0f)
    44.         {
    45.             Die();
    46.         }
    47.     }
    48.  
    49.     void Die()
    50.     {
    51.         Destroy(gameObject);
    52.     }
    53. }
    54.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class helth : MonoBehaviour
    7. {
    8.  
    9.     public Slider slider;
    10.     public int maxHealth = 100;
    11.     public int currentHealth;
    12.  
    13.     public helth helthBur;
    14.  
    15.     private void Start()
    16.     {
    17.         currentHealth = maxHealth;
    18.         //helthBur.SetMaxHealth(maxHealth);
    19.     }
    20.  
    21.     public void SetMaxHealth(int health)
    22.     {
    23.         slider.maxValue = health;
    24.         slider.value = health;
    25.     }
    26.  
    27.     public void SetHealth(int health)
    28.     {
    29.         slider.value = health;
    30.     }
    31.  
    32.     public void getHurt(int ouchies)
    33.     {
    34.         currentHealth -= ouchies;
    35.  
    36.         helthBur.SetHealth(currentHealth);
    37.     }
    38. }
    39.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    The answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  3. Epikmemer45

    Epikmemer45

    Joined:
    Oct 13, 2020
    Posts:
    9
    The problem is that GetHurt void is null. But I don't know how to fix it as it's in the GetHurt void is in the helth class. I want the Enemy class to reference the GetHurt void in the helth class.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    It seems whatever you are giving it MIGHT NOT HAVE that health script on it.

    If this is expected, you need to test for the health script for being null before blindly using it.

    If this is not expected, then that is the actual problem. Get a health script on what it is hitting.

    It really doesn't matter what you're trying to do. The solution is always the same:

    - Identify WHAT is null
    - Identify WHY it is null
    - Fix that.

    To help you untangle messy hairy unreasonable code like line 37 above, refer to this post:

    How to break down hairy lines of code:

    http://plbm.com/?p=248