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
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.
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.
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!
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.
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.