- Home /
Add prefabs from a folder to array as GameObject
the title speaks for itself.
Answer by Bleakmountain50 · Dec 27, 2018 at 04:53 AM
Here you go:
public GameObject[] folderObjects;
public List<GameObject> testingList;
void Start () {
folderObjects = Resources.LoadAll<GameObject>("NAME OF FOLDER");
//Important note: place your prefabs folder(or levels or whatever)
//in a folder called "Resources" like this "Assets/Resources/FOLDERNAME"
foreach (GameObject i in folderObjects)
{
testingList.Add(i);
}
}
I've added in a list just so you can see whats in the array
I'm curious as to why you need to put stuff from a folder into a list. Could you tell me? If you don't want to that fine
Answer by upasnavig90 · Dec 27, 2018 at 05:54 AM
i guess only this line is enough to put objects in an array:
folderObjects = Resources.LoadAll("NAME OF FOLDER");
until and unless all your prefabs are not in same folder.
if prefabs are in different folders, then just load from the resources and add in array or better keep in list because its dynamic.
Your answer
Follow this Question
Related Questions
Create with code 2.2 pizza not shooting 1 Answer
How to tell at runtime if a GameObject is a prefab 3 Answers
Mark gameobject field as changed from prefab 2 Answers
Reinitialize prefab 0 Answers
Apply Changes to Prefab - Unity 2018 0 Answers