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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resources.LoadAll to list. C# Please Help

Discussion in 'Scripting' started by rjudkins, Apr 4, 2013.

  1. rjudkins

    rjudkins

    Joined:
    Apr 1, 2013
    Posts:
    9
    I want to be able to grab prefabs from a folder and put them into a list. Here is what I have

    Code (csharp):
    1. GameObject[] gos = (GameObject[])Resources.LoadAll("Ground");
    2.        foreach(GameObject go in gos) {
    3.         prefabList.Add(go);
    It doesn't actually add them to the list. I do not know where the problem is occuring
     
  2. DougMcFarlane

    DougMcFarlane

    Joined:
    Apr 25, 2009
    Posts:
    197
    Your code seems fine, unless it can't be 'GameObject[]' and needs to be 'Object[]'.

    For reference, here's a part of my WorldManager class that simply loads all my levels (as TextAsset) (stored in "Assets/Resources/Levels/")

    Code (csharp):
    1. public static class WorldManager {
    2.     public const string LevelPath = "Levels";
    3.     private static List<string> worldList;
    4.  
    5.     public static void LoadWorldList() {
    6.         Object[] worlds = Resources.LoadAll(LevelPath, typeof(TextAsset));
    7.  
    8.         // Make sure results aren't empty or null
    9.         if (worlds == null || worlds.Length == 0) {
    10.             // "#WorldManager.LoadWorldList() :: No World files found!"
    11.         }
    12.  
    13.         // Add each result to the worldList
    14.         worldList = new List<string>();
    15.         foreach (Object world in worlds) {
    16.             TextAsset w = (TextAsset)world;
    17.             worldList.Add(w.name);
    18.         }
    19.  
    20.         // Unload them - no longer needed
    21.         foreach (Object world in worlds) {
    22.             Resources.UnloadAsset(world);
    23.         }
    24.     }
    25.  
    Try adding the check for empty / null to see if you are getting any results.
     
    Alphajunge likes this.
  3. rjudkins

    rjudkins

    Joined:
    Apr 1, 2013
    Posts:
    9
    It doesn't seem to say that it's empty, just not adding to the list. Any idea why?
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you might have to post more code.
     
  5. rjudkins

    rjudkins

    Joined:
    Apr 1, 2013
    Posts:
    9
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class pertyMuch : MonoBehaviour {
    6.    
    7.     public List<GameObject> prefabList = new List<GameObject>();
    8.     public int numberOfObjects;
    9.     public Transform player;
    10. //  public GameObject prefab1;
    11. //  public GameObject prefab2;
    12. //  public GameObject prefab3;
    13.     private Vector3 nextPosition;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.         nextPosition = transform.localPosition;
    18.         numberOfObjects = 13;
    19.         GameObject[] gos = (GameObject[])Resources.LoadAll("Ground");
    20.         foreach(GameObject go in gos) {
    21.             prefabList.Add(go);
    22.         }
    23.         //prefabList.Add(prefab1);
    24.         //prefabList.Add(prefab2);
    25.         //prefabList.Add(prefab3);
    26.  
    27.  
    28.            
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.         int prefabIndex = UnityEngine.Random.Range(0, prefabList.Count);
    34.  
    35.         for(int i = 0; i < numberOfObjects; i++) {     
    36.             if(nextPosition.x > player.transform.position.x - 500){
    37.                 GameObject o = (GameObject)Instantiate(prefabList[prefabIndex]);
    38.                 o.transform.localPosition = nextPosition;
    39.                 nextPosition.x -= o.transform.localScale.x;
    40.             }
    41.         }
    42.        
    43.  
    44.         GameObject[] gobs = GameObject.FindGameObjectsWithTag("Ground");
    45.         foreach(GameObject gob in gobs){
    46.             if(player.transform.position.x + 60 < gob.transform.position.x)
    47.                 Destroy(gob);
    48.            
    49.         }
    50.     }
    51.  
    52. }
    53.  
    That's all of it. lol
     
  6. DougMcFarlane

    DougMcFarlane

    Joined:
    Apr 25, 2009
    Posts:
    197
    Try adding a debug line to this code, to see if it is at least finding anything:

    Code (csharp):
    1. foreach(GameObject go in gos) {
    2.     Debug.Log("Adding '" + go.name + "'");
    3.     prefabList.Add(go);
    4. }
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    code actually looks fine to me, but I havn't dabbled in resource loading much, but since its not working, maybe try something like this...

    Code (csharp):
    1.  
    2.  
    3. void Start()
    4. {
    5. foreach(GameObject g in Resources.LoadAll("Ground", typeof(GameObject)))
    6. {
    7.    Debug.Log("prefab found: " + g.name);
    8.    prefabList.Add(g);
    9. }
    10. }
    11.  
    12.  
    Ive intentionality included some debugging so you can be sure its actually finding something.
     
    Walo_de_Cole likes this.
  8. rjudkins

    rjudkins

    Joined:
    Apr 1, 2013
    Posts:
    9
    That worked! I don't know what is different but thank you very much kind sir!
     
  9. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    The thing that may have affected it is the specification of the type. Easiest way to tell would be to try your old code again with the same extra param. Im thinking if this does work, the problem was the cast from Object[] to GameObject[] wasnt working

    Code (csharp):
    1.  
    2.  
    3. GameObject[] gos = (GameObject[])Resources.LoadAll("Ground", typeof(GameObject));
    4.  
    5.  
     
  10. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    It IS very different.

    One thing is to cast an array of objects to the array of GameObjects, and another thing is to loop through elements (and cast each object to GameObject).
     
  11. joelm27

    joelm27

    Joined:
    Feb 4, 2016
    Posts:
    32
    Hello

    Am trying to achieve something similar, but I have to load all images from Resources/Textures... Which they all have format such as png, jpg and other format. After I load it then I have to draw each image with it texture.
    Thanks
     
  12. joelm27

    joelm27

    Joined:
    Feb 4, 2016
    Posts:
    32
    I have to draw those images on a panel.
     
  13. joelm27

    joelm27

    Joined:
    Feb 4, 2016
    Posts:
    32
    Am using unity 5.3, also am receiving an InvalidCastException: Cannot cast from source type to destination type. Precisely where am loading it
     
  14. joelm27

    joelm27

    Joined:
    Feb 4, 2016
    Posts:
    32
    Error on line 3
    Line1: GameObject gameParent = GameObject.FindGameObjectWithTag("pnlHoldCard");
    Line2: GameObject gameCards = GameObject.CreatePrimitive (PrimitiveType.Cube);
    Line3: Texture2D[] Cardtextures = (Texture2D[]) Resources.LoadAll ("");
    //
    foreach (Texture2D ItemLst in Cardtextures) {
    gameCards.GetComponent<Renderer> ().material.mainTexture = ItemLst;
    }
     
  15. DougMcFarlane

    DougMcFarlane

    Joined:
    Apr 25, 2009
    Posts:
    197
    The error is saying your code is returning an asset that is *not* a valid Texture2D file.
    Try putting in only one Texture2D file into that folder and try again.
    This will make sure your code is at least valid.
    Try different graphic files too, in case Unity doesn't support certain formats.
     
  16. joelm27

    joelm27

    Joined:
    Feb 4, 2016
    Posts:
    32
    Hello I solved it, the reason was Resources.LoadAll() return an object then I declared a var variable to hold it.
     
  17. NikitaGaidai

    NikitaGaidai

    Joined:
    Sep 10, 2018
    Posts:
    1
    Edit your code:

    Code (CSharp):
    1. using System.Linq;
    Code (CSharp):
    1. prefabList = Resources.LoadAll<GameObject>("Ground").ToList()
     
    Sehee26 and Nikitty like this.
  18. scradsleep

    scradsleep

    Joined:
    Nov 7, 2019
    Posts:
    21