Shader: Undeclared Identifier

HI there,
I am a Shader newb and I have this error. How do I solve this?

Error:
undeclared identifier ‘_Color’

Shader "sd_silhouetteOnly" {
    Properties {
        _Color ("Color", Color) = (1,1,1,1)
        _OutlineColor ("Outline Color", Color) = (0,0,0,1)
        _Thickness ("Thickness", Range(0.001, 0.1)) = 0.01
    }
    SubShader {
        Tags { "RenderType"="Opaque" }
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
            };

            struct v2f {
                float4 pos : SV_POSITION;
                float3 normal : NORMAL;
            };

            v2f vert (appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.normal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target {
                fixed4 col = _Color;
                fixed3 normal = normalize(i.normal);
                // Compute outline color
                fixed4 outline = _OutlineColor * dot(normal, _WorldSpaceCameraPos);
                // Multiply outline by thickness
                outline *= _Thickness;
                // Add outline to object color
                col.rgb += outline.rgb;
                return col;
            }
            ENDCG
        }
    }
}

You have to define the variable again in your Pass

1 Like

Right, though it’s not really defining it again but actually defining it in the cg / hlsl code section. The “Properties” section is just part of the shaderlab syntax to define the Material properties for the inspector display. The actual shader code is between the CGPROGRAM and ENDCG tags.

Those variables are simply set before the shader is used. In Unity that’s the responsibility of the Material. A Material has a shader and the corresponding parameter values which should be set / used when something is rendered with that material. So everything surrounding the CG section is essentially shaderlab glue, meta data and directives that is used by Unity’s material and rendering system to manage the concept of materials and shaders. The actual shader (the code that runs on your GPU) is defined in the CG sections. They are somewhat seperate / augmented / interleaved.

See the shaderlab documentation for more details and to better understand the general structure. There’s also a page that explains how to access material properties inside a CG shader.

Note that you don’t have to define Properties at all. They are just part of the material, not of the shader. The shader could define a variable that is simply set manually through a C# script and it would still work, even without a Properties section. Though you usually create a Properties section since in most cases you do want inspector support for your properties.

1 Like