Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I add Prefabs from Resources folder to List

Discussion in 'Scripting' started by graviton, Jun 23, 2015.

  1. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    It seems to be unnecessarily difficult.
    I can add to an Array just fine e.g.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3.  
    4. public class listResouresTest01 : MonoBehaviour
    5. {
    6.  
    7.     public GameObject[] myArray;
    8.  
    9.     void Start()
    10.     {
    11.         myArray = Resources.LoadAll<GameObject>("myPrefab");
    12.     }
    13. }
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4.  
    5. public class listResouresTest01 : MonoBehaviour
    6. {
    7.    
    8.     public List <GameObject> myList;
    9.     public GameObject[] myArray;
    10.    
    11.     void Start()
    12.     {
    13.         myArray = Resources.LoadAll<GameObject>("myPrefab");
    14.         myList = myArray.ToList();
    15.     }
    16. }
    Is there a way to add Prefabs directly to a List?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I think it's

    Code (csharp):
    1.  
    2. myList = new List<GameObject>(myArray);
    3.  
     
  3. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    did you even read my post?

    I'm trying to add Prefabs to a "List" without having to add them to an Array first, then to a List

    I don't even want to use an Array
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Not possible. Resources.LoadAll will return an array. However there is no reason you need to make the array a class variable, simply declare it at the method level and let it fall out of scope naturally.
     
    Westland and graviton like this.
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Resources.LoadAll returns an array.

    Resources.LoadAll<GameObject>("myPrefab").ToList()

    if you must, but its still going to allocate that array underneath..
     
    Westland, graviton and Kiwasi like this.
  6. graviton

    graviton

    Joined:
    Jan 11, 2013
    Posts:
    75
    Thanks guys

    BoredMormon
    thanks for the explanation

    JamesLeeNZ
    thanks for the example code

    For whoever has run into to same problem, some code for ya

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6.  
    7. public class listResouresTest01 : MonoBehaviour
    8. {
    9.   public List <GameObject> myList;
    10.  
    11.   void Start()
    12.   {
    13.   //List = Array of "Resources" folder to List
    14.   myList = Resources.LoadAll<GameObject>("myPrefab").ToList();
    15.   }
    16. }
    17.  
    18.  
    more details
    http://answers.unity3d.com/question...m-resources-folder-to-list.html#answer-993418
     
    Last edited: Jun 25, 2015
    Westland likes this.
  7. MindGem

    MindGem

    Joined:
    May 11, 2017
    Posts:
    84
    is ToList() depreciated?
     
  8. Westland

    Westland

    Joined:
    Jan 26, 2015
    Posts:
    26
    Shouldn't be, you're possibly missing the "using System.Linq;" at the top ?