Change Material's Rendering mode at runtime?

I make an rpg game and currently working on attacking system(really simple one). And I want to fade the enemy from opaque to completely transparent after it died. And I found how to change rendering mode at unity doc.

Regarding to this link https://docs.unity3d.com/560/Documentation/Manual/MaterialsAccessingViaScript.html

I do the same thing but it doesn't work at all even I make a copy of the same material and put the fade version to Resources folder. I just wonder why it doesn't work. Or I just have some problem with my interpretation...


Here's my code(short ver.)

private SkinnedMeshRenderer rend;

...

[chec if it's hp <= 0]

StartCoroutine (Fade());

IEnumerator Fade()

{

yield return new WaitForSeconds(1.5f);

rend.material.SetFloat("_Mode", 2);

rend.material.EnableKeyword("_ALPHABLEND_ON");

}

...

[Fade color in Update() method]

  • Jonathan Gonzalez(jgonzalez) replied

    It's hard to understand your shortened version of your script. Where is the "StartCoroutine" being included? Is it in the Start method? At the end of your post you have "Fade color in Update() Method", which seems like it would cause issues but I don't know without seeing the completed code of this section. 

  • Aunnop Kattiyanet(aunpyz) replied
  • Aunnop Kattiyanet(aunpyz) replied

    I forgot to mention that without

    rend.material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
    rend.material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
    rend.material.SetInt("_ZWrite", 0);

    in FadeColor() method, it doesn't work at all. All these lines don't appear in a doc, but in built-in shader code.