Make an Object Invincible

Make an Object Invincible

๐ŸŽฏ Goal

Block all damage for a while, such as spawn protection or a shield power-up.

๐Ÿงฐ What You Need

  • Health
  • Invincibility on 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

FieldWhat it doesSet it to
On Invincibility StartedInvoked when the component is enabledStart a shield or blink effect
On Invincibility StoppedInvoked when the component is disabledStop 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.

๐Ÿ”— Related