Menu
CoddyTech

Damage Everything

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 52 of 64.

challenge icon

Challenge

Medium

Let the sword hurt anything that can be hurt, through an interface.

  1. In IDamageable.cs, declare void TakeDamage(int amount);.
  2. Enemy implements IDamageable: TakeDamage takes amount off health and destroys the enemy at 0 or below.
  3. In Hero.Swing(), replace the tag test and Destroy with hit.TryGetComponent(out IDamageable target) and target.TakeDamage(swingDamage);.

Press Play and swing at a grunt: it takes two hits. Then Check.

Try it yourself

// A later lesson fills this in: anything that can be hurt.
public interface IDamageable
{
}

All lessons in Game Architecture in Unity

Practice on your own: Unity playground