- Home /
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.
Answer by tanoshimi · Jul 04, 2016 at 09:39 PM
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: https://docs.unity3d.com/Manual/class-GraphicsSettings.html
That's why it's way better to create the material in the editor and reference it somewhere. Creating a material on the fly can cause problems if you do that repeatedly. $$anonymous$$aterial instances have to be destroyed manually. If you don't you will cause a memory leak.
Your answer
Follow this Question
Related Questions
Laying a texture over an entire terrain 1 Answer
How can I edit Material Textures without creating a new Shader?,How to edit Material Texture 1 Answer
What are the difference between between Unlit/color and Sprite/default material shader? 0 Answers
Why do materials created at runtime show up incorrectly in the inspector? 1 Answer
Can I have a surface shader and a fragment shader on the same mesh? 0 Answers