unity runtime_error ai_generated true

着色器错误:在'Custom/WaterShader'中:无效的子着色器索引5。子着色器数量为3。

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

ID: unity/shader-invalid-subshader-index

其他格式: JSON · Markdown 中文 · English
90%修复率
86%置信度
1证据数
2024-01-12首次发现

版本兼容性

版本状态引入弃用备注
2021.3 active
2022.3 active
2023.3 active

根因分析

脚本引用的子着色器索引超过了着色器文件中定义的子着色器数量。

English

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

generic

官方文档

https://docs.unity3d.com/Manual/SL-SubShader.html

解决方案

  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.
  2. 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.

无效尝试

常见但无效的做法:

  1. 50% 失败

    Adding unused subshaders bloats the shader and may cause compilation issues; the correct fix is to adjust the index, not the shader.

  2. 80% 失败

    High indices still trigger the same error if they exceed the count; the index must be within range.

  3. 40% 失败

    Shader.Find returns the shader object, but index 0 may not be the intended subshader for all hardware.