Really struggling with the "Object reference not set to instance of an object" error

I’m trying to make an enemy health system for a 2D game I’m cooking up. The basic logic is that upon running out of health, the enemy’s hitbox, rigidbody and scripts disable so that they don’t do anything anymore (therefore making them appear to be dead). The code’s down below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MeleeEnemyHealth : MonoBehaviour
{
public int enemymaxHealth = 10;
public int enemycurrentHealth;
public Bulletlogic bullog;
public bool deadDeactivate;

MeleeEnemyLogic MEL = GetComponent();

// Start is called before the first frame update
void Start()
{
enemycurrentHealth = enemymaxHealth;
}

// Update is called once per frame
void Update()
{

}

public void TakeDamage(int damage)
{
if (enemycurrentHealth > 0)
{
enemycurrentHealth -= damage; // This subroutine damages the player for the amount given at the damaging subroutine
}

if (enemycurrentHealth <= 0) // if health is less than (or equals) 0 then they die
{
EnemyDie();
}
}

void EnemyDie()
{
Debug.Log(“Enemy Just Died”); // death subroutine

GetComponent().enabled = false;
MEL.enabled = false;
this.enabled = false;
}
}

I keep getting the instancing error, and I’ve been searching for hours on how to fix it and still have no clue. Any tips / ideas would be a huge help, thanks!

You don’t provide the details of the error which tells you exactly which line it is on etc. If your reference isn’t set to an object you can then look at where you think you’re setting it.

MeleeEnemyLogic MEL = GetComponent<MeleeEnemyLogic>();

This isn’t valid. You should define this here but assign this in the “Start” method.

MeleeEnemyLogic MEL;

// Start is called before the first frame update
void Start()
{
MEL = GetComponent<MeleeEnemyLogic>();
enemycurrentHealth = enemymaxHealth;
}

Also please use code-tags when posting code and not plain-text.

1 Like

I’ll give this a try, thanks! Also, sorry about the whole code-tags thing, this was my first post on the forum. If I make another one, I’ll remember to use them!

1 Like

Not a problem, just thought I’d point it out. I would say try to post full errors as they indicate both the line and column where the error/warning was found. If you can then indicate where that is in your code, it makes it much easier and quicker to get you help.

1 Like

I hate to be a pain, but I’ve implemented the fix and it hasn’t worked. The same error is now coming from Line 45, which reads as follows:

void EnemyDie()
{
Debug.Log("Enemy Just Died"); // death subroutine

GetComponent<Collider2D>().enabled = false;
MEL.enabled = false;
this.enabled = false;
}

(MEL.enabled = false is Line 45)

The error is here: 6908930--809489--upload_2021-3-7_0-3-0.png

Then post all of the script code again (in code tags) so that we can see what you have done. Just giving us a few lines and saying that the error is on that line does not help us to diagnose what may be wrong with the rest of the script to make this line cause an error.

It should be clear that this is because MEL is NULL so you’ve not correctly assigned the MeleeEnemyHealth object to it. If you did assign it in the “Start()” method then it’ll be because that component doesn’t exist on that specific GameObject.