Unity 5 unified shader after update from Unity 4.x

I am having trouble debugging a Unity app on iOS. There is a function to draw a line on the screen which uses a custom shader. The error I am getting:

NullReferenceException
  at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00000] in <filename unknown>:0 
  at Drawable.InitColor (Color lineColour) [0x00039] in /Users/.../Drawable.cs:269 

      

Line 269 in Drawable.cs:

mLineRenderer.material = new Material(Shader.Find("Custom/LineRender"));

      

The shader code looks like this:

Shader "Custom/LineRender" 
{
    Properties {
    _Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
    }

SubShader {
    Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    Blend SrcAlpha OneMinusSrcAlpha
    Cull Off
    LOD 200
    Pass{
        CGPROGRAM
        #pragma surface surf Lambert

        fixed4 _Color;
        struct Input {
          float2 uv_MainTex;

        };

        void surf (Input IN, inout SurfaceOutput o) {

          o.Albedo = _Color.rgb;
          o.Emission = _Color.rgb; // * _Color.a;
          o.Alpha = _Color.a;
        } 
     ENDCG
    }
  }
} 

      

I don't know much about shaders, so I'm not sure what's going on here. But it worked in Unity 4.6, now it doesn't work in Unity 5. Can anyone see anything obvious that breaks it?

+3


source to share


1 answer


The error indicates to me that Shader.Find("Custom/LineRender")

it cannot return a valid shader.

To make your shader available for loading from your code, you can add it to Always On Shaders. You can find them in the Edit > Project Settings > Graphics

Unity menu.



As for the reason, one can only guess, but the following scenario has happened to me sometimes: your code may have worked "magically" in the past, as one of your compilations may have used a shader. Maybe it was related to stuff in your resources etc. Something has put it into your project and thus made it available for download from your code. When that wasn't there anymore, your code also failed.

+2


source







All Articles