Melee Area Attack - Object reference not set to an instance of an object

Hello everyone. I’m trying to set up a combat system, but I’ve got stuck with a weird error.

using UnityEngine;
using System.Collections;


public class PlayerAttack : MonoBehaviour {

    public float attackTimer;
    public float cooldown;

    // Use this for initialization
    void Start () {
        attackTimer = 0;
        cooldown = 2.0f;
    }
	
	// Update is called once per frame
	void Update () {
        if (attackTimer > 0)
            attackTimer -= Time.deltaTime;

        if (attackTimer < 0)
            attackTimer = 0;

        if (Input.GetKeyUp(KeyCode.F)){
            if (attackTimer == 0)
            {
                FrontAttack();
                attackTimer = cooldown;
                }        
            }

        if (Input.GetKeyUp(KeyCode.G))
        {
            if (attackTimer == 0)
            {
                AreaAttack();
                attackTimer = cooldown;
            }
        }
    }

    private void FrontAttack()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, 2.5f);
        foreach (Collider target in colliders)
        {

            Vector3 dir = (target.transform.position - transform.position).normalized;

            float direction = Vector3.Dot(dir, transform.forward);

            if (direction > 0)
                {
                    EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
                    eh.AddjustCurrentHealth(-10);
                }
            }
        }
}

The function FrontAttack works properly. So I thought “let’s copy an paste the function, name it AreaAttack, change the radius of effect and remove the direction part so it can damage at 360°”.

private void AreaAttack()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, 8f);
        foreach (Collider target in colliders)
        {

                EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
                eh.AddjustCurrentHealth(-5);
        }
    }

But if I try it I get “Object reference not set to an instance of an object” referred to the line which define eh in the AreaAttack function. And if I press “G” repeatedly it damages the enemy, without waiting for the cooldown :frowning:

Here I also paste the EnemyHealth script:

using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour
{
    public int maxHealth = 100;
    public int curHealth = 100;

    // display healthbar
    public int left = 10;
    public int top = 40;

    public float healthBarLenght;

    // Use this for initialization
    void Start()
    {
        healthBarLenght = Screen.width / 2;
    }

    // Update is called once per frame
    void Update()
    {
        AddjustCurrentHealth(0);
    }

    void OnGUI()
    {
        GUI.Box(new Rect(left, top, healthBarLenght, 20), curHealth + "/" + maxHealth);
    }

    public void AddjustCurrentHealth(int adj)
    {
        curHealth += adj;

        if (curHealth < 0)
            curHealth = 0;

        if (curHealth > maxHealth)
            curHealth = maxHealth;

        if (maxHealth < 1)
            maxHealth = 1;

        healthBarLenght = (Screen.width / 2) * (curHealth / (float)maxHealth);
    }
}

I seriously can’t understand why this happens. I would like to know if someone can tell me the reason and maybe offer me another solution (I would like to avoid using something like a list of targets btw).
Thank you a lot for the attention. If you need other parts of my tiny project ask and I’ll be pleased to post them :slight_smile:

This is an easy fix :slight_smile:

The problem is that you are trying to reference a script which doesn’t exist on at least one of the objects in the foreach loop. You find various colliders and then GetComponent on each of them but at least one of them is giving an error.

Solution: right before eh.Adjust double check that you found something for eh.

if (eh!=null){ eh.adjust… }

Let me know if that gets rid of the error!