Fade a character out in Js?

So I browsed this topic, and the only one I can basically understand is this-

var colorStart = Color.red;
var colorEnd = Color.clear;
var duration = 1.0;


var lerp = Mathf.SmoothDamp (Time.time, duration) /duration;
renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);

But how do I apply it to my characters? I understand that they need to have transparent shaders, but how do you do that through script? Also, is there a way to just fade them out without ‘pingponging’ from a specific color?, just lerp to transparent?

Thank you!

EDIT: This code seems to work, but with only the first shader?

var textures = gameObject.GetComponentsInChildren(Renderer);
for (var texts in textures){

texts.material.shader = Shader.Find("Transparent/Diffuse");
texts.material.color.a -= 0.1 * Time.deltaTime;
	}

Why is that?

If you’re already using transparent shaders, you can assign the alpha value of the shader programmatically like so:

Color color = renderer.material.color;
color.a -= 0.1f;
renderer.material.color = color;

From there, you just smoothdamp the value from 1 to 0, or whatever you want to do.

Ok, I’m going to make a separate answer because that one’s getting pretty crowded.

The idea behind using SendMessage is that you can call a script on any of the objects attached to the current game object. In your case, you want to set the shader to transparent. To do so, you would:

  1. create a script, attached to each gameObject that you’re concerned about to set the shader to transparent
  2. when you want to set them all transparent, the gameObject that is acting as the trigger (the parent, presumably) uses

SendMessage(FadeOut, “Transparent/Diffuse”);

This will then prompt the instances of FadeOut (the shader switch script you’ll write and attach to all the children) and feeds it the parameter “Transparent/Diffuse”.

function FadeOut (shader : String) {
    //change your shader with the logic you want here

It should trickle down to any object that is attached to the object calling SendMessage and has the script FadeOut.