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 material will ever use, and then change the property values, either by animating them or from a script.

mat.SetFloat("_HitBlend", 0f); // effect enabled, nothing visible
mat.SetFloat("_HitBlend", 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 materials invisible or turn objects pink 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. 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("_EMISSION_ON");
mat.DisableKeyword("_EMISSION_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 an effect disappears or an object turns pink in a build, this is the first thing to check.
  • 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.

This asset also gives you a third option the others do not: baking the keywords into a dedicated shader variant, which removes the runtime toggling problem entirely by producing a shader with those effects compiled in.

Then test on the target platform. 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, including the sub-toggles, which are only reachable by keyword since they are not material properties.

Keywords in this asset are underscore prefixed: _EMISSION_ON, not EMISSION_ON. Effects with several modes have one keyword per mode rather than an on and off, so a Light Model is switched by enabling _LIGHTMODEL_TOON rather than by toggling a single keyword.