Declaring List variable for subclasses with baseclass or without type?

Hey folks,

I’m trying to find a way to declare a variable that I can save a list of one of a number of subclasses of the baseclass.

I asked a question earlier regarding something similar where you can pass a list parameter (Passing unknown parameter type and class inheritance (C#)? - Questions & Answers - Unity Discussions) - however I can’t figure out if it’s possible to declare a List variable with an unknown type until runtime?

I thought i could save it as it’s baseclass so I’ve tried using:

public List<BaseClass> currentList;

public void storeList(){
    currentList = (a variable with List<SubClass>);
}

But I get a ‘cannot implicitly convert’ error.

I’ve also tried using ‘object’:

    public List<object> currentList;
    
    public void storeList(){
        currentList = (a variable with List<SubClass>);
    }

but same error.

Any help will be gratefully received! :slight_smile:

I figured out at least part of this question. In order to save the SubClass List into a variable List of BaseClass, I had to add them to the BaseClass - not just convert them.

public void loadStuff<T>(List<T> subClassList) where T : BaseClass
    {
        foreach (SubClass s in subClassList )
        {
            currentList.Add(s);
        }
    }