-4

I'm creating an array of Texture so I'm doing it like this

[SerializeField] GameObject[] uitex = new GameObject[4];

void GetTextureFromServer()
{
    string dealer_image = "";
    var tex = new Texture2D(20, 20);

    for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
    {
        dealer_image += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;
        dealer_image += ",";

    }
    string[] NewLinks = dealer_image.Split(',');


    for(int j = 0; j < NewLinks.Length - 1; j++)
    {
        Debug.Log("HERE ARE THE LINKS : " + NewLinks[j]);

        new BestHTTP.HTTPRequest(new System.Uri("***********.amazonaws.com/resources" + "/dealer/pic/" + NewLinks[j]),
            (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res) =>
            {
                tex.LoadImage(res.Data);
            }).Send();
    }

    for (int i = 0; i < uitex.Length; i++)
    {
        uitex[i].GetComponent<UITexture>().mainTexture = tex;
    }
}

What i tried so far is this

uitex[j].GetComponent<UITexture>().mainTexture = tex;

But it gives me an array is out of range and i don't know why.

The problem with this code is that it always gets the last index i have so all 4 gameobject has the same all textures and that's not what i want. Could someone please help me with my problem . Thank you.

Ginxxx
  • 1,602
  • 2
  • 25
  • 54
  • I think you mixed up array sizes. `new GameObject[4]` are 5 elements. Arrays start at 0. Also `uitex[j].GetComponent().mainTexture = tex;` is definitely not possible because `j` isn't in scope. – Smartis has left SO again May 07 '18 at 06:40
  • 1
    You asked this before (few hours ago) and I flagged it as a duplicate. You deleted it and asked it again then provided a duplicate answer....Why? – Programmer May 07 '18 at 12:40

2 Answers2

0

In your second for loop, you load the texture into the tex variable. However you don't do anything with it so when the for loop ends, the tex variable holds the last loaded texture which you set in your third for loop. I'm not sure what your tzPlayInfo.Instance.bc_gametablelist type holds but i would suggest this rewrite:

for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
{
    dealer_image += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;

    Debug.Log("HERE ARE THE LINKS : " + dealer_image);

    new BestHTTP.HTTPRequest(new System.Uri("***********.amazonaws.com/resources" + "/dealer/pic/" + dealer_image),
        (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res) =>
        {
            tex.LoadImage(res.Data);
            uitex[i].GetComponent<UITexture>().mainTexture = tex;
        }).Send();
}

There may be errors in this, as i can't really test it, but i hope you get the idea.

Chimera
  • 149
  • 1
  • 13
-1
for (int i = 0; i < tzPlayInfo.Instance.bc_gametablelist.Count; i++)
    {
        dealer_img += tzPlayInfo.Instance.bc_gametablelist[i].dlrimage;
        dealer_img += ",";
    }
    string[] newLinks = dealer_img.Split(',');

    for (int i = 0; i < newLinks.Length - 1; i++)
    {
        var index = i;  // We need to make a local copy because C# captures variables by reference to lambdas.
        new BestHTTP.HTTPRequest(new System.Uri("***************.amazonaws.com/resources/"
            + "dealer/pic/" + newLinks[index]),
            (BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
            =>
            {
                var tex = new Texture2D(20, 20);
                tex.LoadImage(res.Data);
                uitex[index].GetComponent<UITexture>().mainTexture = tex;
            }).Send();
    }

Came up with this

Ginxxx
  • 1,602
  • 2
  • 25
  • 54