# Shader error in 'Custom/WaterShader': Invalid subshader index 5. Subshader count is 3.

- **ID:** `unity/shader-invalid-subshader-index`
- **Domain:** unity
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

A script references a subshader index that exceeds the number of subshaders defined in the shader file.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 2021.3 | active | — | — |
| 2022.3 | active | — | — |
| 2023.3 | active | — | — |

## Workarounds

1. **Correct the subshader index in the script. For example, change material.shader = shader; material.SetShaderPassIndex(1); to material.shader = shader; material.SetShaderPassIndex(0); where index 0 is the first subshader.** (95% success)
   ```
   Correct the subshader index in the script. For example, change material.shader = shader; material.SetShaderPassIndex(1); to material.shader = shader; material.SetShaderPassIndex(0); where index 0 is the first subshader.
   ```
2. **Add a fallback subshader at the end of the shader file to handle unexpected indices: SubShader { Tags { "RenderType"="Opaque" } Pass { ... } }.** (75% success)
   ```
   Add a fallback subshader at the end of the shader file to handle unexpected indices: SubShader { Tags { "RenderType"="Opaque" } Pass { ... } }.
   ```
3. **Use ShaderVariantCollection to precompile only the needed subshaders, avoiding runtime index errors.** (80% success)
   ```
   Use ShaderVariantCollection to precompile only the needed subshaders, avoiding runtime index errors.
   ```

## Dead Ends

- **** — Adding unused subshaders bloats the shader and may cause compilation issues; the correct fix is to adjust the index, not the shader. (50% fail)
- **** — High indices still trigger the same error if they exceed the count; the index must be within range. (80% fail)
- **** — Shader.Find returns the shader object, but index 0 may not be the intended subshader for all hardware. (40% fail)
