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.
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.
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 documentation.
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
using UnityEngine;
using Zenject;
namespace KrolStudio
{
public class Wall : MonoBehaviour
{
[SerializeField] EnemyHealthBar healthBar;
WallHealth wallHealth;
IPlayerContext playerContext;
[Inject]
public void Construct(IPlayerContext playerContext)
{
this.playerContext = playerContext;
}
void Awake()
{
wallHealth = GetComponent<WallHealth>();
healthBar.Initialize(wallHealth);
wallHealth.Initialize();
}
void OnEnable()
{
// Required so projectiles can detect the wall
gameObject.layer = LayerMask.NameToLayer(GameConstants.Gameplay.EnemyTag);
wallHealth.ResetHealth();
healthBar.UpdateHealthInstant();
healthBar.Hide();
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag(GameConstants.Gameplay.PlayerTag))
{
playerContext.PlayerHealth.TakeDamage(playerContext.PlayerHealth.Current, gameObject, new DamageEventArgs(playerContext.PlayerHealth.Current));
}
}
}
}
namespace KrolStudio
{
public class WallHealth : Health
{
public override void Initialize()
{
base.Initialize();
SetMaxHealth(40);
ResetHealth();
}
}
}
using System;
using UnityEngine;
namespace KrolStudio
{
public class WallDamageImpact : DamageImpact
{
[SerializeField] EnemyHealthBar healthBar;
WallHealth wallHealth;
protected override IHealth Health => wallHealth;
public override void Awake()
{
wallHealth = GetComponent<WallHealth>();
base.Awake();
}
protected override void OnDied(object sender, EventArgs e)
{
base.OnDied(sender, e);
gameObject.SetActive(false);
}
}
}