Confused about one of the methods

For the TakeDamage method I’m confused what int Damage is doing. How is it getting its value?

public class Health : MonoBehaviour
{
    [SerializeField] private int health = 100;

    private void OnTriggerEnter2D(Collider2D other)
    {
        DamageDealer damageDealer = other.GetComponent<DamageDealer>();

        if (damageDealer != null)
        {
            TakeDamage(damageDealer.GetDamage());
            damageDealer.Hit();
        }
    }

    void TakeDamage(int damage)
    {
        health -= damage;
        if (health <= 0)
        {
            Destroy(gameObject);
        }
    }
}

int damage is the parameter for the method. It gets the value from the method calling it, otherwise, you’d have to write a different method for each value of damage you could be taking.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms