Tracking Down A Null

Hi, I am getting an error that so far hasn’t affected gameplay, but I’d still like to know what’s going wrong.

“NullReferenceException: Object reference not set to an instance of an object
DamageSource.Start () (at Assets/Scripts/Player/DamageSource.cs:14)”

Opening DamageSource brings us

public class DamageSource : MonoBehaviour
    
{

    private int damageAmount = 1;

    private void Start()
    {
        MonoBehaviour currentActiveWeapon = ActiveWeapon.Instance.CurrentActiveWeapon;
        damageAmount = (currentActiveWeapon as IWeapon).GetWeaponInfo().weaponDamage;
    }
    private void OnTriggerEnter2D(Collider2D other)
    {
            
        EnemyHealth enemyHealth = other.gameObject.GetComponent<EnemyHealth>();
            enemyHealth?.TakeDamage(damageAmount);
    }
}

Line 14 would be
damageAmount = (currentActiveWeapon as IWeapon).GetWeaponInfo().weaponDamage;

The information is in the scriptable object, and damage is being taken and calculated by the enemies as intended. Any pointers?

3 things on that line can happen

  • currentActiveWeapon is null,
  • currentActiveWeapon does not implement IWeapon, or
  • GetWeaponInfo() is returning a null.

Check all of those

3 Likes

Privacy & Terms