DEV Community

Discussion on: Download Images From a Url Endpoint in Runtime with Unity

Collapse
 
gibranixu profile image
GibranIxu • Edited

Your tutorial is great i think it might just save me from a problem i am having.
But i am unable to save the sprite in an array.
Here is the problematic code:

int i = 0;
foreach(string url in collectableUrls){
  StartCoroutine(GetSprites(collectableUrls[i], (response) => {
                                                collectables[i] = response;} ));
  i++;
}

i am doing a foreach loop with an array of links from which i intend to get my sprites, but instead of assigning it inmediatly to a SpriteRenderer i save it in an array of the same size.

The two problems i have are that, 1. Its only saving the last sprite in 'collectableUrls', and 2. 'i' the index in which it should save the sprite array ' collectables' is the length of the array, while it should be one less than the length to prevent IndexOutOfBounds.

I am terribly confused, i want to use this as a generic method because this one method, i think, it should work for a generic string[] i give it and i should be able to save it in a generic sprite[], since i have to get several sprites with different pourpuses.

I got a brute force solution but i dont like it and i rather use a more elegant, shorter and generic code, instead of one IEnumerator coroutine for each array.

if you could point me into a direction it would mean a lot.
Thanks in advance and sorry for the long answer.