NullReferenceException help

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

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
    public Transform target;
    public float speed = 4f;
    public LayerMask whatIsPlayer;
    public float sightRange;
    public float health = 50f;
    public int ouchies = 20;
    public bool playerInSightRange;
    Rigidbody rig;

    private void Start()
    {
        target = GameObject.Find("Sourkid").transform;
        rig = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
        if (playerInSightRange) Attack();
    }

    private void Attack()
    {
        Vector3 pos = Vector3.MoveTowards(transform.position, target.position, speed * Time.fixedDeltaTime);
        rig.MovePosition(pos);
        transform.LookAt(target); 
    }

    private void OnTriggerEnter(Collider other)
    {

       other.gameObject.GetComponent<helth>().getHurt(ouchies);
    }

    public void TakeDamage (float amount)
    {
        health -= amount;
        if (health <= 0f)
        {
            Die();
        }
    }

    void Die()
    {
        Destroy(gameObject);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class helth : MonoBehaviour
{

    public Slider slider;
    public int maxHealth = 100;
    public int currentHealth;

    public helth helthBur;

    private void Start()
    {
        currentHealth = maxHealth;
        //helthBur.SetMaxHealth(maxHealth);
    }

    public void SetMaxHealth(int health)
    {
        slider.maxValue = health;
        slider.value = health;
    }

    public void SetHealth(int health)
    {
        slider.value = health;
    }

    public void getHurt(int ouchies)
    {
        currentHealth -= ouchies;

        helthBur.SetHealth(currentHealth);
    }
}

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://discussions.unity.com/t/814091/4

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

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.

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