How to instantiate a gameObject not known until runtime?

Hi all. I hope I explain this well…
Say I have game that has 100 playable units and each unit is customisable, e.g. has levels and exp. I’m saving the level and exp info for each unit to a CSV file (for now).

So, let’s say a player has unlocked 20 units, how can I instantiate those specific units if I won’t know what units they are (out of the 100 total) until the data from the CSV is loaded at runtime?

One solution is to instantiate all 100 units and use an identifier to find the 20 units in question and modify them, but this seems so redundant, makes sense if the player has unlocked all 100 units but not for lower numbers.

The Instantiate method requires requires an object to instantiate (obviously), so how can I code it in a way that I don’t know what object will be instantiated? Is resource heavy to have a list containing all unit prefabs, and can I access the id of a prefab to check if it is one of the units I want to instantiate? Maintaining a list of doesn’t seem ideal either.

Any help and/or advice is appreciated.

If you have 100 something units and the player can unlock, then you only need to provide some kind of look-up system to connect the unit’s ID with its prefab.

This could involve loading it via Resources, or addressables, or a direct reference to the prefab. For example you could have a scriptable object that acts as a big look-up table, returning either the prefab, or acting as a factory in and of itself.

Then this ID can be used in your save data to record what the player has unlocked.

And this pattern of using an ID to look up something at a later point of time is kind of a fundamental one to save games in general.

Addressables to the rescue. You can use Addressables for local content (no internet) and that is fine. I use it in every projects.

With Addressables you can assign each prefab a short key name then use it to load at runtime. That’s how you get the prefab to instantiate.

Not less important: The key name should be composable from a given ID - the implementation details are on you though. For example, one of the units is a Car number 10, then you can compose an Addressable key like prefab-unit-car-10 from the ID pair (Car, 10), then pass it to the API Addressables.LoadAsset(key).