My changes

Would it be better to use TryGetComponent at the DamageSource class?

I used this syntax:

        [SerializeField] private int damageAmount = 1;
        private void OnTriggerEnter2D(Collider2D other)
        {
            if (other.gameObject.TryGetComponent(out EnemyHealth enemyHealth))
            {
                enemyHealth.TakeDamage(damageAmount);
            }
        }

Yes, that would work.
But i have another one.
EnemyHealth is NULL when the other has no EnemyHealth on it.

private void OnTriggerEnter2D(Collider2D other)
{
    var EnemyHealth = other.gameObject.GetComponent<EnemyHealth>();
    if (EnemyHealth) EnemyHealth.TakeDamage(damageAmount);
}

Privacy & Terms