It sounds like you're running into the classic "purple shader" error in Unity, which typically means the shader isn't compatible with your current render pipeline. The original shader I provided was indeed written for the Built-in Render Pipeline (BIRP), but if you're using the Universal Render Pipeline (URP) or High Definition Render Pipeline (HDRP), it won't work out of the box. This causes Unity to fall back to a default "error" material, which appears pink/purple.
Don't worry—I'll help you fix this. Since URP is the most common pipeline these days, I'll assume that's what you're using (if not, let me know). We'll need to adapt the shader for URP compatibility. The C# script you provided for swapping materials is fine; the issue is likely just with the shader/material setup.
Step 1: Update the Shader for URP
Here's a version of the shader rewritten for URP. It uses URP's shader includes, tags, and rendering conventions. This should resolve the purple error and make the outline work properly.
Create a new shader file:
- In Unity, go to Assets > Create > Shader.
- Name it something like "CustomOutlineShaderURP".
- Replace the entire content with this code:
Shader "Custom/OutlineShaderURP"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {} // For the main object's texture
_OutlineColor ("Outline Color", Color) = (1, 1, 1, 1) // Color of the outline
_OutlineWidth ("Outline Width", Range(0, 0.1)) = 0.03 // Width of the outline
}
SubShader
{
Tags { "RenderPipeline" = "UniversalPipeline" "RenderType"="Opaque" }
// Main Pass: Render the object normally
Pass
{
Name "Forward"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float2 uv : TEXCOORD0;
float4 positionHCS : SV_POSITION;
};
TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
float4 _MainTex_ST;
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
half4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
return color;
}
ENDHLSL
}
// Outline Pass: Render the outline on top
Pass
{
Name "Outline"
Tags { "LightMode" = "UniversalForward" }
Cull Front // Only render front faces
ZWrite Off // Don't write to the depth buffer
ZTest Always // Always render, even if occluded
HLSLPROGRAM
#pragma vertex vertOutline
#pragma fragment fragOutline
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
};
float4 _OutlineColor;
float _OutlineWidth;
Varyings vertOutline(Attributes IN)
{
Varyings OUT;
float3 normalWS = TransformObjectToWorldNormal(IN.normalOS); // Convert normal to world space
float3 positionWS = TransformObjectToWorld(IN.positionOS.xyz); // Convert position to world space
positionWS += normalWS * _OutlineWidth; // Offset vertices along the normal in world space
OUT.positionHCS = TransformWorldToHClip(positionWS); // Transform back to clip space
return OUT;
}
half4 fragOutline(Varyings IN) : SV_Target
{
return _OutlineColor; // Return the outline color
}
ENDHLSL
}
}
FallBack "Universal Render Pipeline/Lit" // Fallback to URP's default lit shader
}