- Home /
Adjusting alpha cutoff during run time doesn't work
Hello, I am trying to change the render mode of my material on a standard shader to cutout mode with the following code:
mesh_renderer.materials[0].SetFloat("_Mode", 1);
mesh_renderer.materials[0].SetFloat("_Cutoff", 1f);
The code works just fine and the alpha cutoff is set correctly in the inspector, however it doesn't work on the mesh until I drag it to any other value than 1f, and then back to 1f in the inspector, even if I ran the code through update.
Is there a way it would apply the changes to the material from the code, without requiring me to manually do that?
Thanks in advance!
Answer by Thomas-Mountainborn · Aug 17, 2015 at 06:09 PM
A material created at runtime does not have all the necessary standard shader keywords enabled. You need to set a bunch of other ones before you have it set as transparent - by dragging the value in the inspector, the editor does this for you, which is why that works.
material.SetOverrideTag("RenderType", "TransparentCutout");
material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
material.SetInt("_ZWrite", 1);
material.EnableKeyword("_ALPHATEST_ON");
material.DisableKeyword("_ALPHABLEND_ON");
material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
material.renderQueue = 2450;
This post by Farfarer provides a handy method to make your life easier in regards to this issue.