Skip to content

How to Enable/Disable Effects at Runtime

There are 2 ways of achieving this.

Use this one. Almost every effect has a value combination that makes it look deactivated, usually its blend or amount property at 0, though it varies by effect. Polar Coordinates is the exception: it has no properties, so it can only be switched by keyword. 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 for each combination you will switch to, where the build will find it. Unity builds the variants that the materials in the build use: materials on objects in a scene that is in the build (a disabled object is enough), in a Resources folder, or in an Addressables group or AssetBundle. A material that only sits in your project folder is not part of the build. If a sprite disappears in a build, this is the first thing to check: work out which combination it landed on, and make sure a material in the build 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:

The Material Inspector's Color Effects list, with an arrow from 1.Glow to its hover tooltip, which shows the keyword GLOW_ON

From version 4.71 you can also right click the effect, or any of its sub-toggles, and pick Copy keyword, which puts the keyword on the clipboard.

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.