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.