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

Changing multiple UI.Image source image downloaded via WWW class shows last image

Discussion in 'Scripting' started by Paul_aranza, Oct 10, 2015.

  1. Paul_aranza

    Paul_aranza

    Joined:
    Dec 6, 2012
    Posts:
    3
    Hi, having problem with changing source images for multiple UI.image components.

    I have a news Prefab which contains two UI text components and a UI.Image component.
    I do an api call to a server to get the news articles and parse it through simpleJson.
    After which I proceeded to create several instances of the prefab.

    It all went smoothly for one article. However, When my API returns two or more articles, apparently it just downloads/displays the last image in the images returned by the api.

    This is the code I put for filling out the items of the articles:
    Code (csharp):
    1.  
    2.      foreach (JSONClass article in news.AsArray)
    3.                 {
    4.                  
    5.                     Transform item = List.GetChild(article["id"].AsInt - 1);
    6.                     StartCoroutine(getImg(article["Pic"], item));
    7.                     print(article["aPic"]);
    8.                     item.FindChild("Title/Text").GetComponent<Text>().text = article["title"];
    9.                     item.FindChild("short/Text").GetComponent<Text>().text = article["Long description"];
    10.  
    11.                 }
    12.  
    getImg:
    Code (csharp):
    1.  
    2.         IEnumerator getImg(string path, Transform someTransform)
    3.         {
    4.          
    5.             form = new WWWForm();
    6.             form.AddField("path", path);
    7.             Debug.Log(form);
    8.          
    9.             www = new WWW(imgURL, form);
    10.          
    11.             yield return www;
    12.             print("Downloading");
    13.             if (www.isDone)
    14.             {
    15.                 if (www.texture != null)
    16.                 {
    17.                  
    18.                     sprit = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), Vector2.zero);
    19.                     someTransform.FindChild("picture").GetComponent<Image>().sprite = sprit;
    20.                 }
    21.             }
    22.  
    23.         }
    24.  
    Sample result of my api:
    Code (csharp):
    1.  
    2.     [
    3.         {
    4.             id: 1,
    5.             title: "asdafss",
    6.             Long description: "sdfasdfas",
    7.             aPic: "uploads/Articles/32701.jpg"
    8.         },
    9.         {
    10.             id: 2,
    11.             title: "sdrasr",
    12.             Long_desc: "asd",
    13.             Pic: "uploads/Articles/22164.jpg"
    14.         },
    15.         {
    16.             id: 3,
    17.             title: "sample3",
    18.             Long description: "lorem",
    19.             Pic: "uploads/Articles/99777.jpg"
    20.          }
    21.     ]
    22.  
    As it appears it only loads the last picture for **all** of the prefabs I instantiated.
    I do realize this could be due to the coroutine, or maybe I'm missing something.
    Anyone who have done something similar?

    I have posted on Unity answers but as I read on the FAQs discussion should be here, just no sure where exactly should I post.

    EDIT:
    I do a post request to my backend which processes the pic path and returns the image I want
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    To verify which gameobject is actually getting the image payload, in your coroutine that sets the .sprite field, make some debug logging that prints out the name of the gameObject you are on, and perhaps prints the name of the associated resource.

    I theorize that you are perhaps setting each subsequent article to every slot in your scene, and hence only the last one prevails. Printing out names might give you some insight into what's happening.
     
    Paul_aranza likes this.
  3. Paul_aranza

    Paul_aranza

    Joined:
    Dec 6, 2012
    Posts:
    3
    great idea. would try it today. Thanks, would post results