I am setting a renderer material shader to “Transparent/Diffuse” in my code. This has worked until the recent update 4.2.1f4 (don’t recall what I was using previously but I updated a couple of times without issue). It still works fine in Unity but when I build a target (Specifically Mac OS, x86, or 64 bit, or universal), I get pink meshes since they appear to have no renderer. I assume I need to manually add the renderer to the project but I don’t know where to find it. I also can’t locate it in the unity editor when trying to set it for a mesh renderer (extra strange since loading it in code works in the editor, but not in the build).
I have no idea where to go to resolve this… I suspect it has something to do with Unity updating my project with the latest update (which also means I can’t go back to an older version…)
When you changing material from code - make sure you have those shaders in Resources folder. But only custom ones - buildin shaders should always work - even when changing from code.
Maybe made your copy of that shader - call if different, put in Resources folder and try like this.
The issue with copying the shader is that I don’t know where it comes from. It doesn’t show up in the Unity editor any more, the only way it loads is from code executed in the editor. I can’t set it through the IDE, and if I build it doesn’t load from code any more either.
With more investigation it appears to be a problem specifically with OS X builds. Building for web works fine. I put in some logging and when building for OS X targets Shader.Find(“Transparent/Diffuse”); returns null. In the editor (running on OS X for reference) it works fine, and web plugin targets work fine. I suspect this is a bug with the latest Unity build that doesn’t properly copy the base shaders for OS X targets unless they are attached to an object already. Maybe an optimization pass that’s too aggressive or doesn’t take into account that things might just be code based?
My solution is to create a cube, assign a texture to it, then set the shader to Transparent/Diffuse, then disable the object in the editor. This is ugly and hacky but it forces the shader to compile in.
We did get more aggressive about stripping shaders from builds if it looks like they are unused. For a cleaner workaround check out Project Settings → Graphics → Always Included Shaders.
Perfect Kaspar, great to know that a) I’m not going crazy b) my hunch was right and c) there’s a proper fix implemented in the system.
Of note, I was still getting the shader in web exports so presumably those aren’t scrubbing as deeply (I like optimization so now that I know how to work around this I’m a happy camper and would like to see more cleaning on more targets).
The web plugin still ships with those shaders installed to handle backwards compatibility. Since it’s part of the plugin it doesn’t add to the size of the game. It’s of course a bit confusing that web content works differently.