0
\$\begingroup\$

This is how I try to load my sprite images:

 sprite Sprites[];

 void Start() {
    ...
    Sprites = Resources.LoadAll<Sprite>("Card/AnimalImages");
    Debug.Log("Sprite name: " + Sprites[8].name);
    ...
 }

But it gives me the error that you can see in the picture below. Note that in the same figure there is a hierarchy of folders on the right side. I put two ellipsis in the code because it is actually much longer than what I posted, but I assure you that the script is correctly attached to the object and it generally works.

enter image description here

The problem is that it doesn't load the sprites as I expect: the debugger hangs at the point where the Debug.Log(...); is executed (precisely when it tries to access the Sprites vector which is probably empty).

It's the first time in Unity3D where I need to load the sprites in that way. I tried to look in some previous posts, probably I will have missed someone, but in those found until now no one has helped me.

Can someone help me to understand how to load the sprites and especially where is my mistake?


Update

I tried to check if could be a problem of static/dynamic arrays, but it seems not to be a problem of this type, in fact following code:

    Sprites = new ArrayList(Resources.LoadAll<Sprite>("Card/AnimalImages"));
    Debug.Log("Size of dynamic array: " + Sprites.Count);

prints:

Size of dynamic array: 0

\$\endgroup\$
2
\$\begingroup\$

When in doubt, always read the docs for the methods you're using:

Description

Loads all assets in a folder or file at path in a Resources folder.

If path refers to a folder, all assets in the folder will be returned. If path refers to a file, only that asset will be returned. The path is relative to any Resources folder inside the Assets folder of your project.

(Emphasis added)

So when you call Resources.LoadAll<Sprite>("Card/AnimalImages"), Unity looks for files in a folder path like Assets/.../Resources/Card/AnimalImages. Because you have no such path in your project, it finds nothing, and gives you an array of zero length.

If there are assets that you want to load dynamically through code, you need to put them under a folder called Resources so that Unity knows to package them up into the built game, even if it doesn't see them referenced in any scenes.

\$\endgroup\$
  • \$\begingroup\$ That said...Resources has been deprecated in favor of Asset Bundles. Although I have yet to figure out how to use them myself, I still know that much. \$\endgroup\$ – Draco18s Jun 9 '18 at 16:06
0
\$\begingroup\$

Since you don't know the amount of sprites in the folder (i'm assuming you are not checking that). I would recommend to use a Dynamic array instead of a regular one, this comes at the cost of little bit performance but as long as that's ok I see no benefit of having a normal array over a dynamic. With dynamic you can add and remove sprites as you please and it also makes it easier to search etc.

Edit: could also be that the images is not imported as sprites and the Resources.LoadAll finds 0 elements

\$\endgroup\$
  • \$\begingroup\$ Thank you for your answer. I tried to use a dynamic array, but the problem does not seem to be that. Most likely it can not load the elements from the indicated path. I updated my question. \$\endgroup\$ – kitsune Jun 4 '18 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.