Is there a way to constrain a generic type to both Image and Text?

So, I’ve been making fade out and fade in script. At first, I’d only need a function for Text, as I was fading text (as my GUI), however, now I have Images as well, so I attempted to unify the function with a generic type. Easy, I’d thought, until I got an error that I need to put Image before all other constraints. I did that, and the error now prompted me to put Text before all other constraints. So, in a sense, I got into a infinite loop, so to say. One fix creates another problem. The code example I want to make work for both types is:

public IEnumerator FadeInText<TxtOrImg> (TxtOrImg[] Array) where TxtOrImg : Image, Text
	{
		StopCoroutine("ColorShift");
		StopCoroutine("FadeOutText");

		while (Array[0].color.a < 1.0f)
		{
			yield return null;

			foreach (TxtOrImg arr in Array)
			{
				Color temp = arr.color;
				temp.a = Mathf.Lerp(temp.a, 1.01f, 0.05f * FadeInSpeed);
				arr.color = temp;
			}
		}
	}

Are there any workarounds? Such as putting those in an object and going from there? I’m sorry if this is a duplicate but I couldn’t find an answer. I know making 4 functions might be the dirty way around it, but then I also have to make another function that uses the 2 new functions, and that is a drag for something that looks like it could be solved in very few lines of code.

If it isn’t possible, what is the best way to do this aside from separate functions?

Since the color attribute is part of the Graphic class, you can do :

 public IEnumerator FadeInGraphic (Graphic[] Array)
 {
     StopCoroutine("ColorShift");
     StopCoroutine("FadeOutText");

     while (Array[0].color.a < 1.0f)
     {
         yield return null;

         foreach (Graphic arr in Array)
         {
             Color temp = arr.color;
             temp.a = Mathf.Lerp(temp.a, 1.01f, 0.05f * FadeInSpeed);
             arr.color = temp;
         }
     }
 }

Brother that is not posible, you need use method everloading.
will be more clear and readable.

The solution to your problem can be:

  1. method overloading.

  2. Implements an interface (or abstract class) that knows how to do the work of fading and have one method that take
    just that type.