octagon-plusHow to add new Unit (Health & Damage)

Description

This section demonstrates how to add a new interactive unit to the game using a destructible wall that must be destroyed to progress further.

The same approach can be applied to enemies, obstacles, or interactive objects.

Create the Wall GameObject

1. Create the following hierarchy in the scene:

WallContext (Empty GameObject)
├── WallGraphics (Mesh / Cube)
└── EnemyHealthBar (Prefab)

Object descriptions:

  • WallContext Root object for the wall. Contains logic, colliders, and interaction components.

  • WallGraphics Visual representation of the wall (cube or any model).

  • EnemyHealthBar Predefined UI prefab displaying the wall's health.

2. Create MonoBehaviour Scripts

Create the following MonoBehaviour scripts.

Wall.cs

Main wall component. Responsible for:

  • initializing health;

  • linking with the UI Health Bar;

  • interacting with the player;

  • setting the layer for damage detection;

  • integrating with Zenject.

WallHealth.cs

Subclass of the base Health class.

Responsible for:

  • setting the wall’s maximum health;

  • managing health reset.

circle-info

The maximum health value can be modified or exposed via SerializeField for inspector adjustment.

WallDamageImpact.cs

Handles damage and death events.

Responsible for:

  • subscribing to health change events;

  • responding to wall destruction;

  • deactivating the GameObject when health reaches 0.

3. Add Components to GameObjects

WallContext

Add the following components:

  • BoxCollider

    • Is Trigger — enabled

    • Size should match the boundaries of the WallGraphics object

    • Used for:

      • receiving damage

      • detecting player collision

  • Wall

  • WallHealth

  • WallDamageImpact

  • ZenAutoInject Required because the Wall class uses Dependency Injection (IPlayerContext).

The BoxCollider is marked as IsTrigger. This collider is needed to react to damage. Make sure its Size matches the boundaries of the WallGraphics object.

circle-info

The ZenAutoInject component is required because in the Wall class we request the Zenject playerContext from the environment. Read more about ZenAutoInject in the Zenject documentationarrow-up-right.

WallGraphics

  • Add a BoxCollider if the visual model should physically interact with the player.

  • When the player collides with the wall, the player is destroyed (see Wall.OnTriggerEnter method).

If the player model hits a wall, the player explodes. To allow the parts to react to the wall, a BoxCollider was added. (See the Wall class's OnTriggerEnter method)

EnemyHealthBar

  • Use the existing prefab

  • No additional setup is required

Result

After completing all steps:

  • A wall with a health bar appears in the scene

  • The player can shoot the wall to deal damage

  • When health reaches 0, the wall is destroyed (deactivated)

  • If the player collides with the wall, the player is destroyed

Last updated