- Home /
Why does my Unity application can't find the Specular shader after build at runtime?
Solved this by my own in the meanhile (see Update 3 at the bottom). But maybe it's helpful for any Googling Googler, so I'm not going to delete. (though this post is not even released by moderation right now lol)
System-informtions:
HP ProBook 450 G0 (Intel Core i5)
Windows 8.1 64 bit
Unity 4.6.1f1 (free version)
Application built as standalone Windows player
Hint:
(For some reasons, like integrated Occulus SDK, I didn't use my original project for debugging and those screenshots, but the example scene from the author of that obj-loader mentioned later)
Description of my problem:
My purpose was to make Unity to load a .obj 3D model at runtime into my world. For this I used a parser I found at everyday3d.com.
So ... everything seemed to work fine, I put that scripts into my project and was able to load obj files from my webspace, I even managed to add dynamicaly loaded behaviour from outside. Well so the model gets visible within the test environment of the Unity Editor:
But when I build it and start the application, the model doesn't appear at runtime. What I get during debugging the development build is the exception visible in the screenshot:
As far as I can reproduce, the problems begin with
m = new Material(Shader.Find("Specular"));
And yeah well ... I have no clue what happens afterwards. The "Build Agent" directory doesn't exist, when I try to inspect the further "path" of the given exception. I guess that's just some temporary help-code for the Unity interpreter? Anyway my applications seem to not find the Specular shader. I hope someone can help me :/
Update: Hm found something in the output_log.txt of my project (data folder). Can't get something new out of it but maybe you can:
NullReferenceException
at (wrapper managed-to-native) UnityEngine.Material:Internal_CreateWithShader (UnityEngine.Material,UnityEngine.Shader)
at UnityEngine.Material..ctor (UnityEngine.Shader shader) [0x00006] in C:\BuildAgent\work\d63dfc6385190b60\artifacts\StandalonePlayerGenerated\ShaderBindings.cs:149
at OBJ.GetMaterial (.MaterialData md) [0x0000c] in D:\Source\Unity_test4_whereIsMyMonkey\Assets\OBJ\src\OBJ.cs:209
at OBJ.Build () [0x0002a] in D:\Source\Unity_test4_whereIsMyMonkey\Assets\OBJ\src\OBJ.cs:236
at OBJ+c__Iterator0.MoveNext () [0x001e2] in D:\Source\Unity_test4_whereIsMyMonkey\Assets\OBJ\src\OBJ.cs:73
Update 2: According to information inside the Unity Scripting Documentation, Shaders will only be included into the build, if they were used otherwise in any scene or are stored in the Resources directory. So the next question: where are the standard shaders of unity located? Seems like I have to push them into the Resources folder.
Update 3: Ok, this has been the right clue. Quick workaround: added a new material using the Specular shader into a folder named Resources. Thanks for your attention, anyway!
Answer by MazonDel · Jan 28, 2019 at 10:40 AM
I had a similar problem to this and an easier/simpler way to solve the issue is the following.
Go to Edit->Project Settings->Graphics.
In this space find the "Always Included Shaders", extend the list by 1 slot and in the new slot select the shader you need. This will force Unity to always build that shader into the build.
Hope this helps!
Answer by LFCB · Oct 13, 2017 at 12:32 AM
Dude that is an excellent info, I was in the same situation but your post saved me. Thank you very much :D
Answer by rccohn · Nov 01, 2020 at 05:29 PM
This is a great and helpful post. I ran into the same problem (worked OK in the editor, ran into the exception very much like the one above when running the built app).
By extending the shader list with the one I was using, and saving into an Assets/Resources folder, that seemed to fixed the problem. Note that I wasn't sure where to do the assets save when making the change in the Project Settings ... | Graphics tab, and there was no guidance from the save dialog as to where it should be saved. I accepted the default name: NewShaderVariants.shadervariants.
So, I would say that all of the above at least pointed me in the right direction of how to solve the problem.
Thanks again.