Shader giving undeclared identifier error

I’m new to writing shaders, and i’m trying to write a shader that changes color based on proximity to a position that I give it. I give the shader a float4 where the x, y, and z are the position and the w is the distance that the effect has from the position.
Here is my code:

Shader "Unlit/fogofwar"
{
    Properties
    {
       
        _MainTex ("Texture", 2D) = "white" {}
        _PosWithVision("Pos", Vector) = (0,0,0,0)

    }
        SubShader
    {
        Tags { "RenderType" = "Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
           
           
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
               
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float3 worldPos : TEXCOORD0;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                int factor = 0;
                float2 XY = i.worldPos.xyz;

               
                float dist = distance(XY, _PosWithVision.xyz);


                fixed4 col = (dist > _PosWithVision.w) ? 0 : 1;
               
               
                return col;
            }
            ENDCG
        }
    }
}

Basically, the problem is that unity says that there is an undeclared identifier error at line 59 with _PosWithVision, even though I declare it as a property.

While it is defined as a property, the shader code itself has no knowledge of those. You need to define it as a shader “uniform”, a variable that’s defined outside of a function, so the shader knows about it.

Add this line:

float4 _PosWithVision;

Add it somewhere in the shader before the function it gets used in, like just after the float4 _MainTex_ST; line, which is another shader uniform.

4 Likes