Execute Code despite variable/custom-class types (types AS variables)

Consider an Inventory System
My Player has four inventories with four different inventory types (say a, b, c and d).
A, b, c and d are custom classes (variable containers) which all contain a name, sprite and description along with a few other variables that are specific to each class.
I have an Inventory Display GameObject that I wish to be able to display all classes’ name, sprite and description. How might I do this? If you see a flaw in my logic or a potentially better solution, I am all ears.

The code I am basically looking for:

public class Example : Monobehaviour {

  public [interchangeable variable] [] varList;
  public Image[] imageList;
  
  int i = 0;
  foreach ([interchangeable variable] var in varList) {
     imageList[i].sprite = var.sprite;
     i++;
  }
}

All help is appreciated :slight_smile:

You could use an interface to do this. Each of your custom classes would implement an interface that defines the shared properties.

Then your “varList” would use the interface type.

https://learn.unity.com/tutorial/interfaces

The potential downside of this is that Unity can’t serialize interfaces in the inspector. If this is required, then you could use inheritance, with a base class definining the shared properties.

I see, after some research on interfaces and inheritances, I get what you mean and how I could implement this. Half the problem I face is not knowing what I can do in c#/Unity.
Thanks for the help!

Edit: For those who are still looking for another option, I’d suggest generics