- Home /
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.
Please post questions as questions and answers as answers. Don't post the solution to a problem in a question.
Also, all the information you have given is readily available through other sources, so I really don't see the point of more duplicate information... (Duplicate here)
Please be nice to those who try and help. This page was extremely helpful to me.
I thought examples of dynamically adding objects to a list vs. array side by side might be helpful since there don't seem to be many examples of how to write it in context.
Sorry, I will not post in the future.
glasstongue this was very helpful. Thanks so much.
Glass Tongue thank you.
In case anyonewas wondeing I am in Unity 5.3 and the casting is a little different than other examples I have soon. At first I tried
WRONG
$$anonymous$$yObjArr = Resources.LoadAll("path")as Gameobject[];
But Glass Tongue had it right with the cast
RIGHT
$$anonymous$$yObjArr = Resources.LoadAll<Gameobject>("path");
Answer by Journeythedev · Jul 21, 2017 at 07:13 PM
Extremely helpful! Thanks so much, I actually will refer people to this who have this question in class and on my team.
Answer by LogicalCuber · Jun 07, 2020 at 06:24 AM
6 Years later ... and you make another guy happy hahhaha Many thanks!!!
Yeah man same here helped a lot by making my little single slot inventory!