Is it possible to define a variable of multiple types?

I have a class which contains a var “myRenderer”. This class is used in the game and in the UI as well. “myRenderer” is meant to contain the renderer that belongs to the appropriate instance of that class, so that I can easily access the instances visuals.
Now there is the problem, that I ‘visualise’ (the only front-end manifestation of the class instances is via sprites) some instances inside the UI not the normal game, which forces me to use Images instead of SpriteRenderers.
I don’t want to create a second class, that is identical to the existing one (except the type of the myRenderer var), just because I have visualised 5 instances of it inside the UI. I also would have to duplicate every method relying on that class accordingly.
Is there a better solution?

If I understand what you’re looking for correctly I think you could just use a generic method to get your variable. If it’s a variable of a type that inherits from Component, as I believe SpriteRenderer and Image do, then both types can be stored there and the generic method will return it back with the type specified, much like the GetComponent method in Unity.

public class ContainerClass : MonoBehaviour {

    public Component myRenderer;    

    public T GetMyRenderer<T>()
    {
        return (T)(object)myRenderer;
    }

}

It would work if there was a common base class, but unfortunately that’s “object” and therefore not even capable of doing anything graphic related. I don’t think there is a way since there’s just no common behaviour.