Invincibility
Invincibility makes the Health on the same GameObject ignore all damage while the component is enabled. Turn the component on to become invincible, and off to take damage again.
It isn’t a setting on Health. It’s a health modifier: a separate component that hooks into Health and gets a say on every hit before health changes. Invincibility simply answers “0 damage” to every hit.
✅ Ideal For
- Spawn protection after a respawn
- Short invulnerability after taking a hit
- Cutscenes, dodges, shields or power-ups
⚙️ How It Works
flowchart LR
A["Damage"] --> B["Invincibility"] -->|"0 damage"| C["Health unchanged"]
- Enabled: it registers itself with the
Health, so every call toHurtasks it first, and it returns0. - Disabled: it removes itself, and damage goes through normally.
- Healing doesn’t go through modifiers, so an invincible object can still be healed.
- Hits on a hitbox still raise the hitbox’s On Hit, so hit effects still play while invincible.
📐 Inspector Overview
| Field | Description |
|---|---|
| On Invincibility Started | Raised when the component is enabled. Start a blink, shield or glow effect here |
| On Invincibility Stopped | Raised when the component is disabled. Stop the effect here |
Note
Invincibility requires a Health on the same GameObject.
🎮 Turning It On and Off
Because it works by being enabled or disabled, anything that can toggle a component can drive it:
- Animation: keyframe the component’s enabled checkbox. The Simple example’s player intro animation enables it when the ship spawns and disables it 3 seconds later.
- UnityEvents: hook an event to
Invincibility.enabled. - Code:
using System.Collections;
using UnityEngine;
using GlowingRifts.ModularHealthSystem;
public class SpawnProtection : MonoBehaviour
{
[SerializeField] private Invincibility _invincibility;
[SerializeField] private float _duration = 2f;
public IEnumerator Protect()
{
_invincibility.enabled = true;
yield return new WaitForSeconds(_duration);
_invincibility.enabled = false;
}
}Tip
Keep the component disabled in the prefab if the object shouldn’t start invincible.