# Fading back and fourth Unity Image alpha

I'm trying to create a way in which if I press a button, it'll highlight other objects whilst at the same time fading other objects. I also have other buttons that will fade and highlight different objects. Some of which may have been modified by the first button.

I had done this originally in a really dumb way like so

public void Highlight()
{
foreach (Image image in imagesToHighlight)
{
Color c = image.color;
if(c.a < maxColor)
{
c.a = maxColor;
}

image.color = c;
}

{
Color c = image.color;
if(c.a > halfColor)
{
c.a = halfColor;
}
image.color = c;
}
}


This works, but I was wanting it to happen gradually to provide a smother transition. I managed to get the fade working, by changing the code to this:

IEnumerator Fade(Image image)
{
float elapsedTime = 0.0f;
Color c = image.color;
while (elapsedTime < 1)
{
elapsedTime += Time.deltaTime;
c.a = 1.0f - Mathf.Clamp01 (elapsedTime / fadeTime);
image.color = c;
}
}


I then call the method in my foreach loop like so:

foreach (Image image in imagesToFade)
{
}


Again, this works fine. The problem I'm having is checking to see if the image is faded, and if it is and needs to be highlighted, it reverses from the fade value to the highlight value (0.5 to 1.0).

I thought this would be a simple change as did this

IEnumerator HighLight(Image image)
{
float elapsedTime = 0.0f;
Color c = image.color;

while (elapsedTime < 1 )
{

• You'll want to use Mathf.Lerp: docs.unity3d.com/ScriptReference/Mathf.Lerp.html – Jesse Williams Oct 4 '16 at 13:31