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

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);

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.