- Home /
C# Shader.Find not working
I am writing in C#. My project is running perfectly in the builder but after i build it for PC i tells me nullreferenceexception. i commented all my code little by little and i found that the problem line is this one:
Material myMat = new Material(Shader.Find("Transparent/Diffuse")); (in the update method)
the script was in the Assets folder, i tried moving it to Standard Assents and still doesnt work.
I think that my scripts are in the wrong place or there is some building option that i don't know of.. But it is extremely strage that in the builder when i click the play button there is no problem.... no matter where i place the scripts and other things...
Any advice will be tested and appreciated :)
Answer by GameVortex · Jan 09, 2014 at 11:52 AM
I am not sure, but it might be that because Unity does not include assets in the build that you have not reference directly it has stripped out the Transparent/Diffuse shader. So the Shader.Find function can not find it. Try making a new material in your assets folder that has the Transparent/Diffuse shader on it, assign this material in the inspector to a public material variable in your script. Then you can make a new material in your update method by copying the referenced one.
Something like this:
Declare and assign a Meterial variable:
public Material material;
Then copy it when you want to create a new Material.
Material myMat = new Material(material);
Also I forgot about this one: A somewhat better way is to edit Grahpic settings where you can add the Shaders you want the build to include no matter what. Go to Edit - Project Setting - Graphics. Increase the number by one and press the little circle on the right to bring up a list of all shaders. Select the on you want and it will be included in the build.
Answer by Kolodej · Jan 09, 2014 at 02:46 PM
When the shader/material is not attached to any of the scene objects, then is not considered for compilation (i.e. works only in editor). There are 2 ways how to resolve the problem: 1. Move the shader to Resources directory. 2. Attach the maretial to some scene object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125929im_/https://answers.unity.com/themes/thub/images/avi.jpg)