-
::When my weapon hit the enemy it showed this error.
I tried using Debug.Log(“Enemy health:” + enemyData.Maxhealth) at EnemyController script and yes it shows the enemy health
next I’m using Debug.Log(“Enemy take damage: ” + currentDamage) and at ProjectileWeaponBehavior script and it shows enemy take the damage. But I tried to Debug.Log(“Enemy: ” + enemy) but it showed nothing. What do I miss 🤔🤣
::<code>protected virtual void OnTriggerEnter2D(Collider2D col) //col = collider
{
if (col.CompareTag("Enemy"))
{
EnemyStats enemy = col.GetComponent<EnemyStats>();
Debug.Log("Enemy: " + enemy);
enemy.TakeDamage(currentDamage); <== Error Here
Debug.Log("Enemy take damage: " + currentDamage);
}
}</code>
::I already set a tag as “Enemy”
::Here’s TakeDamage function
<code>
void Awake()
{
currentHealth = enemyData.Maxhealth;
}
public void TakeDamage(float dmg)
{
currentHealth -= dmg;
if (currentHealth <= 0)
{
Dead(); <== Destroy game object
}
}</code>
::isaac, the error is caused by this line:
EnemyStats enemy = col.GetComponent<EnemyStats>();
If the line fails to find an EnemyStats
component, then the enemy
variable will be empty. This is why enemy.TakeDamage()
has an error, because it evaluates to null.TakeDamage()
.
Check that the enemy prefabs in your game all have an EnemyStats
component.