Adding prefabs to a list or an array from a folder and instantiating them

Thanks to a few of the awesome members of Answers I managed to populate an array with prefab objects and instantiate them in my scene (from a folder in in my project) - then figured out I shouldn’t use an array and figured out how to do it in a list.

Sharing it here because this information was so hard to find despite it being pretty simple to implement (plus it helps me solidify how it works in my mind.)


How to populate an Array with Prefabs from a folder (If you need to remove objects from your collection of prefabs use a list):

//define an Array
public static GameObject[] myObjects;

//I used this to keep track of the number of objects I spawned in the scene.
public static int numSpawned = 0;
  
void Start()
{
    //Important note: place your prefabs folder(or levels or whatever) 
    //in a folder called "Resources" like this "Assets/Resources/Prefabs"
    myObjects = Resources.LoadAll<GameObject>("Prefabs");
}

void SpawnRandomObject() 
{    
    //spawns item in array position between 0 and 100
    int whichItem = Random.Range (0, 100);
    GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
    numSpawned++;
    myObj.transform.position = transform.position;
}

void Update() 
{
    if (numToSpawn > numSpawned) 
    {
        //where your instantiated object spawns from
        transform.position = new Vector3(0, 0, 0);
        SpawnRandomObject ();
    }
}

How to populate a List with Prefabs from a folder:

//define a list
public static List <GameObject> myListObjects = new List<GameObject>();

//I used this to keep track of the number of objects I spawned in the scene.
public static int numSpawned = 0;
  
void Start()
{
    //Important note: place your prefabs folder(or levels or whatever) 
    //in a folder called "Resources" like this "Assets/Resources/Prefabs"
    Object[] subListObjects = Resources.LoadAll("Prefabs", typeof(GameObject));

    //This may be sloppy (I've only been programing for a short time) 
    //It works though :) 
    foreach (GameObject subListObject in subListObjects) 
    {    
        GameObject lo = (GameObject)subListObject;
        myListObjects.Add(lo);
    }
    startPosition = transform.position;
}

void SpawnRandomObject() 
{    
    //spawns item in array position between 0 and 100
    int whichItem = Random.Range (0, 100);
    GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
    numSpawned++;
    myObj.transform.position = transform.position;
}

void Update() 
{
    if (numToSpawn > numSpawned) 
    {
        //where your instantiated object spawns from
        transform.position = new Vector3(0, 0, 0);
        SpawnRandomObject ();
    }
}

Ok! Hopefully this makes this information easier to find - if you have suggestions to make this better let me know.

Also - if anyone can point me to a good explanation of using enums to sort that would be awesome.

Extremely helpful! Thanks so much, I actually will refer people to this who have this question in class and on my team.

6 Years later … and you make another guy happy hahhaha
Many thanks!!!

4 More years later and another guy happy :slight_smile: thx