3

I'm having some trouble with debugging an Unity app on iOS. There is a function to draw a line on the screen, which uses a custom shader. The error I get is:

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 is:

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

The shader code is as follows:

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 is breaking it?

3
  • 3
    I'd say it just can't find the shader at all. Have you tried adding the shader to the "Always included shaders" at Edit > Project Settings > Graphics ? Commented Jun 25, 2015 at 16:04
  • Brilliant! that was exactly the problem, thanks so much! If you add it as an answer I can do the confirmation if you want? Commented Jun 25, 2015 at 20:05
  • I'm glad that helped. I've turned it into an answer. Commented Jun 25, 2015 at 20:57

1 Answer 1

2

The error indicates to me that Shader.Find("Custom/LineRender") fails to return a valid shader.

To ensure that your shader is available for you to load from your code, you can add it to the "Always included shaders". You can find these at Edit > Project Settings > Graphics in Unity's menu.

As for the cause, one can only guess, but the following scenario has happened to me on occasion: your code may have "magically" worked in the past, as in, one of your prefabs may have used the shader. It may have been assigned to a material in your resources, etc. Something pulled it into your project and thereby made it available for you to load from your code. When that no longer was the case, your code failed as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.