'Create a coroutine to fade out different types of object

Hi I'm trying to create a coroutine in unity that will handle fading on all manner of object. So far I'm able to get the alpha values of the different types I want but I'm at a dead end as to how to set the new color after lerping it. Here is my code :

public static  IEnumerator FadeTo(float aValue, float aTime,GameObject gameObj)
            {
                Component[] allComponents = gameObj.GetComponents(typeof(Component));
                Component fadableComponent;
                Type type = null;
                float alpha=0;
                foreach(Component c in allComponents)
                {
                    if(c.GetType()==typeof(Image))
                    {
                        fadableComponent = c;
                        alpha = fadableComponent.GetComponent<Image>().color.a;
                        type = fadableComponent.GetType();
                        Debug.Log("Found a image");
                        break;
                    }
                    if (c.GetType() == typeof(Renderer))
                    {
                        fadableComponent = c;
                        alpha = fadableComponent.GetComponent<Renderer>().material.color.a;
                        type = fadableComponent.GetType();
                        break;
                    }
                    if (c.GetType() == typeof(SpriteRenderer))
                    {
                        fadableComponent = c;
                        alpha = fadableComponent.GetComponent<SpriteRenderer>().color.a;
                        type = fadableComponent.GetType();
                        break;
                    }
                    if(c.GetType()== typeof(CanvasGroup))
                    {
                        fadableComponent = c;
                        alpha = fadableComponent.GetComponent<CanvasGroup>().alpha;
                        type = fadableComponent.GetType();
                    }
                    Debug.Log(alpha.ToString());
                }


                for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
                {
                    Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha, aValue, t));
                    gameObj.transform.GetComponent(type) // What now ?!
                    yield return null;
                }
                yield return null;
            }
        }

Now i know how to do this with lets say another set of if's or what not, but I want to avoid that. For example of solutions I want to avoid I took a look at the iTween implementation. Here is a perfect example of what I want to avoid:

if(target.GetComponent<GUITexture>()){
            target.GetComponent<GUITexture>().color=colors[3];
        }else if(target.GetComponent<GUIText>()){
            target.GetComponent<GUIText>().material.color=colors[3];
        }else if(target.GetComponent<Renderer>()){
            target.GetComponent<Renderer>().material.color=colors[3];
        }else if(target.GetComponent<Light>()){
            target.GetComponent<Light>().color=colors[3];   
        }

This is a mess, moms spaghetti. Extending this will be a nightmare.



Solution 1:[1]

FYI regarding this specific question:

Be aware of crossFadeAlpha , recently added to Unity. It is confusing but it can work well. Be aware that canvas renderers always have an alpha.

public static class MoExtensions
    {

    public static void FadeIn(this Graphic g)
        {
        g.GetComponent<CanvasRenderer>().SetAlpha(0f);
        g.CrossFadeAlpha(1f,.15f,false);
        }

    public static void FadeOut(this Graphic g)
        {
        g.GetComponent<CanvasRenderer>().SetAlpha(1f);
        g.CrossFadeAlpha(0f,.15f,false);
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fattie