Dynamically assigning a material doesn't work on mobile?

Hi everybody. What I’m trying to do is to dynamically create an empty object, add mesh filter, mesh renderer and a material. And so far everything works on PC. Even when I assign material like this:

NewObj.material=new Material(Shader.Find("Unlit/Color"));

But when I run it on my phone, the object is invisible.

Strangely, if I manually add an object to the scene with “Unlit/Color” shader in the editor, it works on mobile AND dynamic object starts working too. This makes me think that I need to somehow preload that shader before I use it dynamically, right?

UPD. I’ve tried creating a material and assigning it instead of creating it in code. And it works. But I’m just curious if I can make it work with just one C# file. Why the shader doesn’t work on mobile if it’s created dynamically.

If no material references a shader at compile-time, it won’t be included in the build. So your mobile build won’t find the unlit shader since it’s only referenced in code.

To fix this, add the unlit shader to the “always included shaders” list on the graphics settings for your project: Unity - Manual: Graphics