Skip to content

How to Make a Sprite Flash White When Hit in Unity

A sprite flashes white when its shader blends every pixel toward white for a moment and then back. Unity’s default sprite shader cannot do that: a SpriteRenderer’s Color multiplies the texture, so it can darken or tint a sprite but never make it brighter than it is. A hit flash needs a shader with a flash colour and an amount you move from 1 back to 0 in about a tenth of a second.

The soldier hit three times in quick succession, twice. Through the SpriteRenderer's Color, set to red on each hit, it only darkens to a red tint. With Hit Effect it flashes white on every hit

Why tinting the sprite cannot flash it white

Section titled “Why tinting the sprite cannot flash it white”

The colour you see is the texture’s colour times the renderer’s Color. White in the Color field leaves the sprite as it is, and every other value makes it darker or tints it. So setting spriteRenderer.color = Color.white on a hit does nothing, and Color.red gives a red tint rather than a flash.

Without a shader, the two usual workarounds are:

  • A white frame in the animation: an all-white copy of the sprite, played for a frame or two. It costs one extra frame per sprite and an animation event per hit.
  • A white silhouette on top: a child sprite with the same shape in white, switched on for the flash. It costs a second sprite per enemy, and it has to follow every frame of the animation.

In Shader Graph, make a Sprite Unlit graph (or Sprite Lit, under URP’s 2D Renderer) and add two properties: a Flash Color and a Flash Amount slider from 0 to 1. Then:

  1. Sample Texture 2D with _MainTex.
  2. A Lerp from its colour to Flash Color, with Flash Amount as T, into Base Color.
  3. The texture’s alpha into Alpha, so the flash keeps the sprite’s shape.

There is no need for a Vertex Color node: URP’s sprite targets multiply the graph’s result by the renderer’s Color on their own, so a Vertex Color node would apply it twice. It also means the Color tints the flash: only a renderer whose Color is white flashes pure white.

The Sprite Unlit graph: _MainTex into Sample Texture 2D; its RGBA into a Lerp's A, Flash Color into B and Flash Amount into T; the Lerp into Base Color and the texture's A into Alpha

Then a script moves Flash Amount. To flash one enemy and not every enemy with the same material, set it through a MaterialPropertyBlock, which gives this renderer its own value without making a new material:

using System.Collections;
using UnityEngine;
public class FlashByHand : MonoBehaviour
{
private static readonly int FlashAmount = Shader.PropertyToID("_FlashAmount");
[SerializeField] private float duration = 0.12f;
private SpriteRenderer sr;
private MaterialPropertyBlock block;
private void Awake()
{
sr = GetComponent<SpriteRenderer>();
block = new MaterialPropertyBlock();
}
public void Flash()
{
StopAllCoroutines();
StartCoroutine(FlashRoutine());
}
private IEnumerator FlashRoutine()
{
for (float t = duration; t > 0f; t -= Time.deltaTime)
{
SetAmount(t / duration);
yield return null;
}
SetAmount(0f);
}
private void SetAmount(float amount)
{
sr.GetPropertyBlock(block);
block.SetFloat(FlashAmount, amount);
sr.SetPropertyBlock(block);
}
}

The property’s Reference in Shader Graph has to match the name in the script, here _FlashAmount.

The soldier with the graph above, flashed white three times by the FlashByHand script, each flash fading over 0.12 seconds

With All In 1 Sprite Shader’s Hit Effect

Section titled “With All In 1 Sprite Shader’s Hit Effect”

Don’t own it yet? → Get it on the Unity Asset Store

Add the AllIn1Shader component to the sprite (Add Component → AllIn1SpriteShader → AddAllIn1Shader) and tick Hit Effect in the material’s Color Effects. It blends the sprite toward Hit Effect Color times Glow Intensity by Hit Effect Blend:

11.Hit Effect ticked in the material's Color Effects: Hit Effect Color white, Glow Intensity 5 and Hit Effect Blend 1, the defaults
  • Hit Effect Color (_HitEffectColor) is the flash colour, white by default.
  • Glow Intensity (_HitEffectGlow) multiplies it, from 1 to 100, 5 by default. Above 1 the flash is brighter than white, which only shows as a halo with Bloom; without Bloom it looks plain white. See How to Make a Sprite Glow.
  • Hit Effect Blend (_HitEffectBlend) is how much of it shows, from 0 to 1. It is 1 by default, so the sprite turns white the moment you tick the effect: set it to 0 for the resting state.
The flash held for a moment three times. At Glow Intensity 1 it is plain white. At 5 under Bloom it is brighter than white and a halo spreads around the soldier. At 5 with Bloom off it is plain white again

Leave the effect on and move only the blend. That way the shader never has to switch variants in the middle of a fight, as How to Enable/Disable Effects at Runtime explains. Scripting has a complete flash script, the one recorded at the top of this page, and How to Animate Effects covers the Animation window and DOTween:

mat.DOFloat(0f, "_HitEffectBlend", 0.12f).From(1f);

Hit Effect is applied before Gradient, Contrast and Brightness, Color Swap, Color Ramp, Change 1 Color, Posterize, Hue Shift and Overlay in the shader. If any of those is on, it works on the flash too: a Color Ramp turns the white into the ramp’s brightest colour and loses the glow, and a Change 1 Color whose target is white replaces the flash with its new colour.

Two ways out:

  • Turn the other effect down for the flash. Color Swap and Color Ramp have a Blend slider (_ColorSwapBlend, _ColorRampBlend); move it to 0 while Hit Effect Blend is up. Change 1 Color has no Blend slider, so for it use the second way.
  • Move Hit Effect below them in the shader. In AllIn1SpriteShader.shader, cut the block that starts with #if HITEFFECT_ON and contains col.rgb = lerp(col.rgb, _HitEffectColor.rgb * _HitEffectGlow, _HitEffectBlend);, and paste it just above the line //OUTLINE-------, after Hue Shift and Overlay. The flash then covers every colour effect. Each shader variant has its own copy of the block: the Scaled Time and UI Mask shaders in their own files, the URP 2D Renderer and SRP Batcher variants in ShaderLibrary. An update of the asset puts it back, so note it somewhere.
Four soldiers flashing. With Change 1 Color targeting white the flash turns green. With Color Ramp it turns the ramp's white and loses its glow. With the ramp's Blend taken to 0 during the flash, it glows white. With the Hit Effect block moved below them, the flash covers both effects and glows

Every sprite with the same material shares it, so a change made through sharedMaterial flashes all of them at once. Three ways to flash one:

  • renderer.material, which makes this renderer its own copy the first time you touch it. Simple, and the copy lives until the object is destroyed. See material or sharedMaterial.
  • A MaterialPropertyBlock, as in the script above, with _HitEffectBlend in place of _FlashAmount. No copy is made, and it works on any renderer. It does not work on UI Images.
  • A material per enemy type, made in the editor, if each type flashes as a group anyway.

The AllIn1Shader component makes a new material for each object you add it to in the editor, and keeps it in the scene until you save it (Saving Prefabs). Once a saved material is on many prefabs, they share it.

A character built from separate body parts has one renderer per part. To flash it as one, give the script an array of the parts’ renderers and set the blend on each, with material or a property block. A single shared material for all the parts, moved through sharedMaterial, also flashes them together, but it flashes every other character using that material as well.

Hit Effect works on a UI Image too, with three differences:

  • UI materials are shared by every Image that uses them, and the Animator cannot animate them. Give the Image its own material on Awake and drive it from code or DOTween (UI Images, UI materials cannot use the Animator).
  • On a Screen Space Overlay canvas the flash shows but never glows, because Bloom does not reach that canvas (why). If the Image disappears altogether instead, something is writing over the depth buffer: set Z Test Mode to Always in the material’s Advanced Configuration and keep Depth Write off, its default (Builds and devices).
  • Inside a Mask or Scroll View, write to image.materialForRendering, not image.material.

The same flash on 3D models and in your own graphs

Section titled “The same flash on 3D models and in your own graphs”

The All In 1 3D Shader has the same effect, called Hit, with Hit Color, Hit Glow and Hit Blend. All In 1 Shader Nodes has a Hit node for your own Shader Graphs.

  • Flash on its own. Hit Effect has no timer: something has to move Hit Effect Blend, a script, an animation or a tween.
  • Flash a UI Image through the Animator. UI materials are shared, so Unity does not let the Animator animate them.