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!