How to manage a list of instantiated prefabs inside a script?

I’m fairly new to Unity an game development, so this problem is probably mostly based on my lack of understanding of some core concepts.

Let’s say, I’m creating a card game. I have a prefab of a “Card” game object (that has a sprite and script attached, to store some values and allows some card-specific actions). I then have a sort of “Card Controller” that handles managing the whole card collection, e.g. creating it at startup, and putting part of them into a deck to draw from etc.

Thus, I wanted to put a list into my Card Controller, maybe like this:

private List<Card> cards = new List<Card>();

Then I could create Card instances and store them in the list, to retrieve and potentially remove later. However, Unity then tells me that this is not acceptable:

You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

Now, I am a bit confused about this as, in my understanding, I’m not actually trying to create a Card instance here, just a collection to store potential instances (which I could create via Instantiate).

I seem to have a fundamental misunderstanding about handling collections of such game objects, but I’m not quite sure where to look to find a better way to approach this. I hope someone could give me some pointers!

What you are trying to do should work fine - that is, to define a List, initialize it, and later on assign elements to it through something like var card = Instantiate(CardPrefab) & cards.Add(card.GetComponent()). Sample code below:

// The GameObject prefab containing your "Card" MonoBehaviour
public GameObject cardPrefab;
// Define the list to hold the cards in the deck
private List<Card> cards = new List<Card>();

void Start(){
    // Instantiate a bunch of cards
    for(int i = 0; i < 100; i++){
        // Note that tempCard here is a GameObject
        var tempCard = Instantiate(cardPrefab);
        // Use GetComponent to get the reference to the Card MonoBehaviour inside the prefab
        cards.Add(tempCard.GetComponent<Card>();
    }
}

Essentially yes, that error is telling you the truth, in unity you cant create instances of scripts (like your card script) that aren’t part of an instantiated object. You can create prefabs in the editor, and assign them to a public or serialised list while the game is not running, but it’s not possible to create prefabs at run time. If you want to create "potential"cards to put in your list, you need to either create prefabs before the game starts, Instantiate objects and hide/disable them till they are needed, or create a new class that isn’t a monobehavior (not a unity script) to hold the “potential” card info before you Instantiate.