How to switch the material on character death

I have a character, and when the die method is called, I want to switch the material to a material that has a shader,how can i do it?

  1. Create a public Material field in your character class.
  2. From the Editor drag your shader material to this field.
  3. In the Die method calls SetMaterial() and pass it the value of the Material field you just added.

You can switch the material that a GameObject is using by assigning a new material to the GameObject’s renderer.

If the game object has only one material:

var renderer = characterGameObject.GetComponent<Renderer> ();
renderer.material = someMaterial;

If the game object has multiple materials:

var renderer = characterGameObject.GetComponent<Renderer> ();
var materials = renderer.materials;
materials[0] = someMaterial; //Change the 1st material
renderer.materials = materials;