Make an Object Invincible
๐ฏ Goal
Block all damage for a while, such as spawn protection or a shield power-up.
๐งฐ What You Need
HealthInvincibilityon the same GameObject
๐ช Steps
Add Invincibility
Add Add Component > Glowing Rifts > Modular Health > Invincibility to the same GameObject as Health. Uncheck the component if the object shouldn’t start invincible.
Turn it on and off
While the component is enabled, every hit is blocked. Disable it and damage goes through again. Toggle it from an animation (keyframe the component’s enabled checkbox), from a UnityEvent (Invincibility.enabled) or from code:
using System.Collections;
using UnityEngine;
using GlowingRifts.ModularHealthSystem;
public class SpawnProtection : MonoBehaviour
{
[SerializeField] private Invincibility _invincibility;
[SerializeField] private float _duration = 3f;
private void OnEnable() => StartCoroutine(Protect());
private IEnumerator Protect()
{
_invincibility.enabled = true;
yield return new WaitForSeconds(_duration);
_invincibility.enabled = false;
}
}Show it to the player
Hook On Invincibility Started to a blink, glow or shield effect, and On Invincibility Stopped to turn it off.
๐ Inspector Settings
| Field | What it does | Set it to |
|---|---|---|
| On Invincibility Started | Invoked when the component is enabled | Start a shield or blink effect |
| On Invincibility Stopped | Invoked when the component is disabled | Stop the effect |
๐งช Example
DS_Player in the Duck Shooter scenes has Invincibility unchecked in the prefab. The ship’s intro animation enables it as the ship flies in and disables it 3 seconds later, so a soap that hits you right after a respawn does nothing.
๐ก Tips
- Healing skips modifiers, so an invincible object can still be healed.
- Hitboxes still raise On Hit while invincible, so hit effects keep playing. The
Health’s On Damage Data Received only fires when health actually changes. - Disabling the whole GameObject also disables the component and raises On Invincibility Stopped.
- For partial protection, such as armor that halves damage, write your own modifier.