Resouces.LoadAll List error [Solved]

I am trying to import a load of Meshes into a List via ‘Resouces.LoadAll’ however it keeps throughing back the following error. I dont understand. I suspect its something to do with the fact its a imported asset with a Mesh?

public class Smithing : MonoBehaviour {

    public static List<Mesh> SwordSkins = new List<Mesh>();
    public void Start()
    {
        SwordSkins.Add(Resources.LoadAll("FantasyCraft Assets/Items/Weapons/Sword Skins", typeof (Mesh)));
    }
}

I’m pretty new to unity, but i’m pretty sure it order to use Resources.Load(), your files must be under a Folder named “Resources”
so, try moving “FantasyCraft Assest” to a new folder called “Resources”.

and to answer why you’re getting this error, it’s probably cause the method returns Null

Resources.LoadAll(string, type) returns Object[ ]
try using
Resources.LoadAll(“path/to/mesh”); instead

1 Like

Didnt work, same error

Ah, Add doesn’t take collections, tried AddRange?

Resource.LoadAll returns an Object[ ], an array of Objects. And you can’t Add this Object[ ]-array as a list item to your List, this is what the error message is telling you. You have to loop through the Object[ ]-array instead and add all of its elements individually “as Mesh” to your List

This is why I vaguely suggested using LoadAll instead, which returns an array of type T.
List.Add(T) only takes a single object as input, List.AddRange takes a collection such as a built-in array returned by LoadAll.

1 Like

Unity seems happy with this, however its now claiming no items are in Object[ ]

    public void Start()
    {
        Object[] meshes = Resources.LoadAll("Sword Skins");
        for(int i = 0; i == meshes.Length; i++)
        {
            Debug.Log("meshes.Length = " + meshes.Length); //Returns 0, it shouldnt
            SwordSkins.Add(meshes[i] as Mesh);
            Debug.Log("Swordskins.count = " + SwordSkins.Count + ". i = " + i);
            Debug.Log("Swordskins.X = " + SwordSkins[i]);
        }
    }

Your for-loop is mesed up - iterating with i == meshes.Length means that the for loop will only run if the array is empty.

If the length is zero, it means that LoadAll couldn’t find the objects. Have you put the Sword Skins folder directly under the Resources folder?

Ah, got it cheers mate.