Change material rendering mode in runtime

Hi, I’m trying to change the rendering mode of material to transparent when my player is near the object. Here is what I got so far.

using UnityEngine;
using System.Collections;

public class ChangeAlpha : MonoBehaviour
{
	public Renderer targetObject;
	public float alpha = 0.25f;

	Color newColor;

	void Awake ()
	{
		targetObject = targetObject.GetComponent<Renderer> ();
	}

	void OnTriggerStay (Collider other)
	{
		if (other.gameObject.tag == "Player")
		{
			newColor = targetObject.material.color;
			targetObject.material.SetFloat("_Mode", 3f);
			newColor.a = alpha;
			targetObject.material.color = newColor;
		}
	}

	void OnTriggerExit (Collider other)
	{
		if (other.gameObject.tag == "Player")
		{
			newColor.a = 1f;
			targetObject.material.SetFloat("_Mode", 0f);
			targetObject.material.color = newColor;
		}
	}
}

This code is not working, but when I change the material rendering mode to transparent before run time and remove the material.setfloat, it was working. but I want the material to be opaque when not trigger. So how can I do this?

I’m sorry to necro this answer, but I just wanted to clarify in case anyone else came across this question.

  1. This answer assumes that you are using the Unity Standard shader.
  2. If you compile and view the Standard shader code, you can see at the very end that it uses a CustomEditor of type “StandardShaderGUI”
  3. If you peek at that code (which others have done), you’ll see that it does more than just change the “_Mode” of the shader.

Here is a helper class that can perform the actions of the StandardShaderGUI at runtime assuming the Material you pass in is using the Unity Standard shader:

using UnityEngine;

public static class StandardShaderUtils
{
    public enum BlendMode
    {
        Opaque,
        Cutout,
        Fade,
        Transparent
    }

    public static void ChangeRenderMode(Material standardShaderMaterial, BlendMode blendMode)
    {
        switch (blendMode)
        {
            case BlendMode.Opaque:
                standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                standardShaderMaterial.SetInt("_ZWrite", 1);
                standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
                standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
                standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                standardShaderMaterial.renderQueue = -1;
                break;
            case BlendMode.Cutout:
                standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
                standardShaderMaterial.SetInt("_ZWrite", 1);
                standardShaderMaterial.EnableKeyword("_ALPHATEST_ON");
                standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
                standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                standardShaderMaterial.renderQueue = 2450;
                break;
            case BlendMode.Fade:
                standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
                standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                standardShaderMaterial.SetInt("_ZWrite", 0);
                standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
                standardShaderMaterial.EnableKeyword("_ALPHABLEND_ON");
                standardShaderMaterial.DisableKeyword("_ALPHAPREMULTIPLY_ON");
                standardShaderMaterial.renderQueue = 3000;
                break;
            case BlendMode.Transparent:
                standardShaderMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
                standardShaderMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
                standardShaderMaterial.SetInt("_ZWrite", 0);
                standardShaderMaterial.DisableKeyword("_ALPHATEST_ON");
                standardShaderMaterial.DisableKeyword("_ALPHABLEND_ON");
                standardShaderMaterial.EnableKeyword("_ALPHAPREMULTIPLY_ON");
                standardShaderMaterial.renderQueue = 3000;
                break;
        }

    }
}

The other important thing to note is that while this will always work in the Editor, you will want to make sure that you have a shader variant that uses the blendmode you want referenced somewhere so that when you build, it will work. You can see if Unity has automatically grabbed the shader variant you need by going to Project Settings->Graphics and at the bottom under Shader Preloading click “Save to asset”. You need a variant under Standard that has _ALPHAPREMULTIPLY_ON defined for the “Transparent” mode to work (which you can see from the above script) as well as all the other defines that the shader is using.

If you don’t see a variant you need listed, you can run your program, make all the changes to the shader, and click this button to save a new ShaderVariantCollection as it tracks variants at runtime. You can then add this collection the the Preloaded Shaders array and it should be added to the build as far as I can tell (haven’t tested currently).

I guess you are missing the keywords to activate the rendering mode!
Have a look here:

and here…

Hope that helps!!