Nullreference in player attack ?

Golly. I’ve been fiddling with this for a few hours now. I’m super new to this so if someone can explain whats wrong I would appreciate it.

NullReferenceException: Object reference not set to an instance of an object
Playermovement.Update () (at Assets/Playermovement.cs:94)

From my understanding I am trying to call the code under DamageToEnemy in the script attached to the foe that is inside of the circle set by the 2D Collider but it pops that error message.

The enemy still takes the damage but I get an annoying error in console.
Thanks bunches

THE ENEMY SCRIPT

public class EnemyHealthManager : MonoBehaviour
{
    public int dmg;
    public int health;
    private void Update()
    {
        Debug.Log("damage taken= " + dmg + ". health= " + health);
    }
    public void DamageToEnemy(int dmg)
    {
       
       
        if (health <= 0.01f)
        {
            Destroy(gameObject);
            //KILL enemy;
        }
        else { health -= dmg; };
    }

}

PLAYER SCRIPT ERROR AREA

if (timebtwattack <= 0.01f)
                {
                    timebtwattack = starttimebtwattack;

                    anim.SetBool("Attacking", true);
                                                      
                    Collider2D[] DmgToEnemy = Physics2D.OverlapCircleAll(attackpos.position, attackrange, whatisenemies);

                    for (int i = 0; i <= DmgToEnemy.Length; i++)
                    {
                        //enemyhealthmanager coming back null?
                        DmgToEnemy[i].gameObject.GetComponent<EnemyHealthManager>().DamageToEnemy(dmg);
                    }
                }

I’m kind of learning as I go so any pointers would be super!

Hi and welcome.
Does every object in the ‘whatisenemies’ layer have an EnemyHealthManager component? Because if that’s not guaranteed, that would be causing the error. It would also explain why your enemies still take damage despite the exception. One other explanation for this would be that you had duplicate scripts, one where the error occurs and one where it doesnt, but since this is your Player script i guess you wouldnt have two of them.

If the error is being thrown due to some ‘whatisenemies’ not having EnemyHealthManager components, then you can work around this problem by separating the objects based on whether they have this component or not:

// inside the for loop
EnemyHealthManager healthManager = DmgToEnemy[i].gameObject.GetComponent<EnemyHealthManager>();
if(healthManager != null){
    healthManager.DamageToEnemy(dmg);
} else {
    Debug.Log("Object " + DmgToEnemies[i].gameObject.name + " is in whatisenemies Layer but has no EnemyHealthManager component!"); // you can throw this out once you are not interrested in this informaiton anymore
}

The Debug.Log() message is just supposed to help you figure out which objects, if any, exist in your whatisenemies group that do not contain a EnemyHealthmanager component. You are free to throw it out afterwards. On that note, Debug.Log is a very powerful tool for debugging and quickly getting some information on a problem. For more complex problems you may want to use a proper debugger / tool, however i hardly ever find this necessary. Maybe i’m just lazy tho.