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

2 Answers 2

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\$
1
  • \$\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\$ Commented Jun 9, 2018 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\$
1
  • \$\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
    Commented Jun 4, 2018 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .