Creating a list of prefabs indexed by enums

Hello!

First question here:)

I’m trying to create a list of prefabs with an enum as the index so that i can easily and quickly instantiate these prefabs from code. The list will be quite long if i manage, i want to put every game object there. The intended use would be somehting like this:

public List<GameObject> myPrefabs = new List<GameObject>();

public GameObject CreateGraphic(Graphic aGraphic)
{
   GameObject newGraphic = GameObject)GameObject.Instantiate(myPrefabs[(int)aGraphic]);
        return newGraphic;
}

EDIT: The question is, how do i fill the List with Prefabs in the enum order? I realize i that if i make the list public i can drag and drop prefabs in the editor, but that seems unreliable to me as there is no actual connection to the enum, just very careful and mistake prone ordering.

What i hope to find is a way to find these prefabs from code so i can enter them in the correct positions at Start(), using the enum as index.

Is this possible in any way? I tried with public dictionaries and hash tables, no luck. Am i going about this the wrong way?

If your enum is going from 0 to x, you can use (int)theEnum. But if it starts at 5, or if the order can change, you can use a dictionary to link the enums with an index.

Dictionary<Graphic, int> dico = new Dictionary<Graphic, int>();

dico.Add( Graphic.something, 0 );
dico.Add( Graphic.somethingElse, 1 );
...

I think you are looking for

using System.Collections.Generic; //need to include this to use List


List<GameObject> myprefabs = new List<GameObject>();

You can then drag them into the inspector.