Skip to content

How to Enable/Disable Effects at Runtime

There are 2 ways of achieving this.

Use this one. Every effect has a value combination that makes it look deactivated, usually its blend or amount property at 0, though it varies by effect. So the clean way is to enable in the editor every effect the object will ever use, and then change the property values, either by animating them or from a script.

mat.SetFloat("_HitEffectBlend", 0f); // effect enabled, nothing visible
mat.SetFloat("_HitEffectBlend", 1f); // fully on

Nothing recompiles, nothing hitches, and the material keeps rendering with the same shader variant all the way through. See Scripting for a worked example.

Section titled “Option 2: Toggle the shader keywords (not recommended)”

This other way is less efficient and messier, and it can make sprites invisible in the final build.

Here is why. Each set of enabled effects is a separate compiled shader variant, and Unity only builds the variants your project actually uses. Switching any keyword, on or off, moves the material onto a different variant, and if nothing in your project uses that particular set, it was never built and the sprite renders as nothing. The editor compiles variants on demand, so this always looks correct in play mode and only shows up on the device.

Material mat = GetComponent<Renderer>().material;
mat.EnableKeyword("GRADIENT_ON");
mat.DisableKeyword("GRADIENT_ON");

If you go this route, every combination you can end up on has to be in the build:

  • Keep a material in your project for each combination you will switch to. Unity builds the variants its materials use, so a saved material with that exact set of effects guarantees the variant ships. If a sprite disappears in a build, this is the first thing to check: work out which combination it landed on, and make sure some material in the project uses it.
  • Include the variants explicitly, through a Shader Variant Collection added to the preloaded shaders in Graphics settings, or whatever variant inclusion your project already has.

Then test on the target platform. Neither of these is something to reach for casually, so unless you understand how Unity strips shader variants and you have a reason option 1 will not work, stay on option 1.

Every effect keyword is listed on Effects and Properties Breakdown, and you can also read one off the Material Inspector by hovering the effect:

Sub-toggles like OUTBASE8DIR_ON, OUTTEX_ON and GLOWTEX_ON are set the same way, and keywords are the only way to reach them since they are not material properties. The variant caveat above applies to them just as much.

These are local keywords, so they are set per material with material.EnableKeyword. Shader.EnableKeyword does not reach them.