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?
- Create a
public Materialfield in your character class. - From the Editor drag your shader material to this field.
- In the
Diemethod callsSetMaterial()and pass it the value of theMaterialfield 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;