Jarrah Technology logo

January 25, 2013

Category: News

Author: Charles

Unity Shader Loading Twitter Facebook Share on Google+

After working mainly inside the Unity3D environment for the last couple of months, I decided to create a standalone build. It didn’t work! Some mouse clicks did not seem to be detected, while others produced the expected results. I went back to Unity3D and everything worked fine there. Unity and it’s standalone build must work in slightly different ways. Luckily the standalone build produces an output\_log.txt in the project’s \_Data directory. Normally this contains very little, but in my broken build the following lines were repeated.

NullReferenceException
  at (wrapper managed-to-native) UnityEngine.Material:Internal_CreateWithShader (UnityEngine.Material,UnityEngine.Shader) 
  at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00000] in <filename unknown>:0  
  at VectorLineUtil.GetLineMaterial () [0x00000] in <filename unknown>:0  
  at TargetPlanner..cctor () [0x00000] in <filename unknown>:0 
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for TargetPlanner

Thus the problem is because the standalone can’t find the Shader specified in a Shader.Find() call. According to the documentation for the API call, shaders are only included in the build if they are used in a material or in the Resources directory. The shader causing the error was installed in the Plugins folder (as it was part of a plugin bought from the Unity Asset Store).

Just moving all the shaders into the Resources directory fixed the issue. A handy tip to remember when structuring your Unity3D project.

Comments: I have removed the commenting system (for privacy concerns), below are the comments that were left on this post before doing so…

test @ 2013-03-29 - thank you!

MF @ 2013-07-02 - We had just spent at least 30 minutes trying to figure this out. Thanks a ton!

Максим Таран @ 2015-06-11 - Edit->Project settings->Graphics-> ..add you custom shader

Александр Корчуганов @ 2016-05-04 - Thank you very much, dude!

Kleber Lopes da Silva @ 2016-06-16 - Thanks for that!

Alessandro @ 2017-09-28 - I was going crazy, thanks!

Tags: Unity