Changing material in code doesn't update shader

I am trying to generate a series of Materials, all from on a base texture with varying Albedo detail textures applied to it (decals).

Currently to test this, I have it generating a single material with the very first decal on it and then applying it to an object. The result is that it does this correctly… but it does not show in game with the new shader unless I manually go in an open the shaders detail panel.

void Update()
			_isAddNew = false;

			// Get the mesh renderer, as this is the renderer type used on _objectToApplyTo.
			MeshRenderer newRenderer = _objectToApplyTo.GetComponent<MeshRenderer>();
			// Set the material to use a Standard shader.
			newRenderer.material = new Material(Shader.Find("Standard"));

			// Albedo. Getting the base material, then the main texture from it.
			newRenderer.material.mainTexture = _materials[0].GetMaterial().mainTexture;
			// Detail Mask
			// Detail Albedo x2

			_objectToApplyTo.GetComponent<MeshRenderer>().material = newRenderer.material;

Here is a step by step to help better show this.

I am using Unity 5.0.0f4 Personal Edition.

Thank you for any help and advice.

It seems the Standard Shader is ‘smart’ about what parts of the shader are enabled, and you need to use material.EnableKeyword() to tell it to turn on a feature if you’re changing things through code. In my case I was assigning a normal map via the “_BumpMap” parameter, which worked after I added the following line to my code:


The comprehensive way to do this is to use the code written by Unity that is executed when you make changes to the material in the Editor. The code is available for download from UnityCsReference/Editor/Mono/Inspector/StandardShaderGUI.cs at master · Unity-Technologies/UnityCsReference · GitHub. Then, open up the StandardShaderGUI.cs file and copy out the five functions at the bottom (SetupMaterialWithBlendMode, GetSmoothnessMapChannel, SetMaterialKeywords, MaterialChanged, SetKeyword). Also copy the enums at the top of the class (WorkflowMode, BlendMode, SmoothnessMapChannel) and put in your own class. Then you want to call MaterialChanged() with the WorkflowMode.Metallic on your Material that you modified. Works identically to in-Editor changes :).

Confirmed in 5.1.1f as well. Something doesn’t update until you click the inspector.