Hi there,
I’m working on the damage system for my game and I have an Enemy abstract class which is inherited by all enemy type scripts, in this example it’s the Snail.
I have my system set up so that when the collider of a weapon object of a player collides with an enemies collider and they are pressing the “Attack” button, it calculates the damage and reduces the enemies health accordingly.
To do this, the weapon is using:
void OnTriggerStay2D(Collider2D other)
{
if (other.gameObject.tag == "Enemy" && Input.GetButtonDown("Attack"))
{
enemy = other.gameObject;
if(character.alignment == "Artisan")
{
mainStat = character.luk;
atkType = character.atk;
defType = other.GetComponent<Enemy>().GetDef();
}
damage = damageScript.CalculateBasicDamage(character.atk, mainStat, defType);
enemy.SendMessage("EnemyDamaged", damage, SendMessageOptions.DontRequireReceiver);
Debug.Log("Enemy Hit.");
}
The problem I’m having is that when this part of the script runs, I get a NullReferenceException at the GetComponent line. The enemy being collided with has the Snail script attached which inherits from Enemy, so I thought I’d be able to use GetComponent to get Enemy and use the GetDef() method to return the Snail’s defense for the calculation, but it doesn’t work. If I replace “Enemy” with “Snail” in GetComponent it does work however.
This wouldn’t be too big a deal if the above snippet didn’t need to be applicable to every monster in the game, but it wouldn’t be practical to have to make a gigantic if statement for every monster in order to get their defense stat.