- Home /
How do I add Prefabs from Resources folder to List
It seems to be unnecessarily difficult. I can add to an Array just fine e.g.
using System.Collections;
using System.Collections.Generic;
public class listResouresTest01 : MonoBehaviour
{
public GameObject[] myArray;
void Start()
{
myArray = Resources.LoadAll<GameObject>("myPrefab");
}
}
But can't seem to figure out the syntax for a List
Right now if I want to add Prefabs to a List I do it this way e.g.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class listResouresTest01 : MonoBehaviour
{
public List <GameObject> myList;
public GameObject[] myArray;
void Start()
{
myArray = Resources.LoadAll<GameObject>("myPrefab");
myList = myArray.ToList();
}
}
Is there a way to add Prefabs directly to a List?
Answer by zach-r-d · Jun 24, 2015 at 07:45 AM
Try wrapping the .LoadAll() call with the constructor of List that takes an IEnumerable, like so:
myList = new List<GameObject>(Resources.LoadAll<GameObject>("myPrefab"));
Then there's no need for myArray.
Answer by kmgr · Jun 24, 2015 at 10:54 AM
myList = Resources.LoadAll<GameObject>("myPrefab").ToList();
Answer by graviton · Jun 25, 2015 at 12:21 AM
Both answers are correct
thanks guys
more details here http://forum.unity3d.com/threads/how-do-i-add-prefabs-from-resources-folder-to-list.335722/#post-2174892
Your answer
![](https://koobas.hobune.stream/wayback/20220613201327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Make the audio manager select from an array of sounds 1 Answer
List all children in array? 5 Answers
Preventing Items from shifting in a list 1 Answer
List changes affecting Array 2 Answers