Can I right a generic method for components having same member variable?

Hello,

I am trying to write a static enumeration function that makes transparent components that having color member variable. While I know how to write a generic function, I couldn’t figure it out how to change color variable of T type object.

Here is my three fade method for image, rawimage, and text components. So basically I want to make these functions into one with a generic method.

  public static IEnumerator<float> _fadeInfadeOut(GameObject rawImage, float speed)
    {
        RawImage r = rawImage.GetComponent<RawImage>();

        Color textureColor = r.color;
        float a = textureColor.a;



        if (a == 0)
        {
            while (a < 1)
            {
                a += Time.deltaTime * speed;
                textureColor.a = a;
                r.color = textureColor;
                yield return 0;
            }
            textureColor.a = 1;
            r.color = textureColor;

        } else
        {
            while (a > 0)
            {
                a -= Time.deltaTime * speed;
                textureColor.a = a;
                r.color = textureColor;
                yield return 0;
            }
            textureColor.a = 0;
            r.color = textureColor;

        }

    }

    public static IEnumerator<float> _fadeInfadeOutImage(GameObject image, float speed)
    {
        Image i = image.GetComponent<Image>();

        Color textureColor = i.color;
        float a = textureColor.a;



        if (a == 0)
        {
            while (a < 1)
            {
                a += Time.deltaTime * speed;
                textureColor.a = a;
                i.color = textureColor;
                yield return 0;
            }
            textureColor.a = 1;
            i.color = textureColor;

        }
        else
        {
            while (a > 0)
            {
                a -= Time.deltaTime * speed;
                textureColor.a = a;
                i.color = textureColor;
                yield return 0;
            }
            textureColor.a = 0;
            i.color = textureColor;

        }

    }

    public static IEnumerator<float> _fadeInfadeOutText(GameObject text, float speed)
    {
        Text t = text.GetComponent<Text>();

        Color textureColor = t.color;
        float a = textureColor.a;



        if (a == 0)
        {
            while (a < 1)
            {
                a += Time.deltaTime * speed;
                textureColor.a = a;
                t.color = textureColor;
                yield return 0;
            }
            textureColor.a = 1;
            t.color = textureColor;

        }
        else
        {
            while (a > 0)
            {
                a -= Time.deltaTime * speed;
                textureColor.a = a;
                t.color = textureColor;
                yield return 0;
            }
            textureColor.a = 0;
            t.color = textureColor;

        }

    }

@umurcg If you have a look at each of those types in Visual Studio, they all inherit MaskableGraphic, so that’s the type you can filter on for the generic. Also, I don’t think your IEnumerator requires a generic type, since you’re always returning zero. The following ought to do:

public static IEnumerator<T> _fadeInfadeOut(GameObject obj, float speed) where T: MaskableGraphic
{
     T t = obj.GetComponent<T>();
 
     Color textureColor = t.color;
     float a = textureColor.a
 
     if (a == 0)
     {
         while (a < 1)
         {
             a += Time.deltaTime * speed;
             textureColor.a = a;
             t.color = textureColor;
             yield return null;
         }
         textureColor.a = 1;
         t.color = textureColor;
 
     }
     else
     {
         while (a > 0)
         {
             a -= Time.deltaTime * speed;
             textureColor.a = a;
             t.color = textureColor;
             yield return null;
         }
         textureColor.a = 0;
         t.color = textureColor;
     }
 }

If you do need to do something with the zero you return, you can add a second generic type parameter, as I’m sure you’re aware.