Fading multiple objects using same hotkey

So I took a script for the forums for fading the alpha on an object and expanded on it. What this code is for is taking an object from transparent to fully opaque and back using Lerp. The problem I have is once I apply this to multiple objects if I try to use the same hotkey for all of them it ignores the Lerp completely and just jumps from transparent to opaque instantly. I know my code is probably not very efficient, I’m extremely new at this but I was wondering if anyone knew how to fix this?

#pragma strict

 var startAlpha : float = 0; //Beginning Transparency
 var alpha : float = 0; //Value that determines the transparency during fade
 var tParam : float = 0; //Parameter to stop/start fade
 var speed : float = 0.3; //Speed at which object will fade
 var reverse : float = 1; //Value to determine whether the object will go from transparent to opaque or reverse. 
 							//Value of 1 equals transparent to opaque

function Start () //Sets Beginning Transparency based on which direction the fade will be going.
{
 	
 	if (reverse >= 1)
 	{
 		startAlpha = 0;
 	}
 	else
 	{ 
 	 	startAlpha = 1;
 
 	}
 	 renderer.material.color.a = startAlpha;
}

 	
function Update() //Assigning "t" and "y" to fade and changing direction of the fade repectively.
{
	if (Input.GetKeyDown("t")){ //Starts Fade, redirects to function decider
		decider();
		}
	if (tParam > 1) { //Cancels the Fade once tParam reaches over 1
 		CancelInvoke();
 			}
 	if(Input.GetKeyDown("y")) //Used to change direction of the Fade
 	{
 		changeAlpha();
 	}		
 	if(Input.GetKeyDown("u")) //Used to Repeat Previous Fade
 	{
 		repeat();
 	}
 }
 	function lerpAlpha() //Fade Function
 	{
 

 		 if (tParam < 1) {
     			tParam += Time.deltaTime * speed;
     			alpha = Mathf.Lerp(0, 1, tParam);
    	 		renderer.material.color.a = alpha;
 		
 		}
 
	}
	function lerpAlphaReverse() //Reversed Fade Function
 	{
 

 		 if (tParam < 1) {
     			tParam += Time.deltaTime * speed;
     			alpha = Mathf.Lerp(1, 0, tParam);
    	 		renderer.material.color.a = alpha;
 		
 		}
 
	}
	 function decider() //Decides between lerpAlpha and lerpAlphaReverse based on reverse Value
 	{
 		if (reverse >= 1)
		{
		
 			InvokeRepeating("lerpAlpha", 0, .00002);
 			
 		}
 		else 
 		{
 			InvokeRepeating("lerpAlphaReverse", 0, .00002);
 			
 		}
	}
	function changeAlpha() //Resets tParam and Alpha to allow Fade to run again. Sets next Fade direction based on previous Fade
	{
		if (startAlpha >= 1)
		{
			startAlpha = 0;
			reverse = 1;
			tParam = 0;
			alpha = 0;
			renderer.material.color.a = startAlpha;
		}
		else 
		{
			startAlpha = 1;
			reverse = 0;
			tParam = 0;
			alpha = 0;
			renderer.material.color.a = startAlpha;
		}
	}
 	function repeat() //Used to Repeat Previous Fade if Desired
 	{
 		if (startAlpha >= 1)
 		{
			startAlpha = 1;
			reverse = 0;
			tParam = 0;
			alpha = 0;
			renderer.material.color.a = startAlpha;
		}
		else 
		{
			startAlpha = 0;
			reverse = 1;
			tParam = 0;
			alpha = 0;
			renderer.material.color.a = startAlpha;
		}
	}

you can add the fade code to the objects and then call them from a controller script

//object with tag "Fader" and script name fadeObject

public bool isFadeIn = false;
public bool isFadeInOut = false;


void Update()
{
if(isFadeIn){ fadeIn(); }
if(isFadeInOut){ fadeInOut(); }
}

public void fadeIn()
{
//your fade code here
}


public void fadeInOut()
{
//your fade code here
}





// on the controller script


GameObject[] fadeThings;

void Start()
{
fadeThings = GameObject.FindGameObjectsWithTag("Fader");
}


void Update()
{
 if(input.getKeyDown(KeyCode.A)) // or whatever
 {
  foreach(GameObject _fader in fadeThings)
  {
   fadeObject temp = _fader.gameObject.GetComponent<fadeObject>();
   fadeObject.isFadeIn = true;
  }
 }
}

sadly i dont have time to check this in unity this morning, but hopefully t should point you in the right direction.

all the best :smiley:

Awesome, Thank you so much! I’m fairly new to coding so this will help a ton!