# Why is my texture array getting only the last link index?

Here's my code:

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

void GetTextureFromServer()
{
string dealer_img = "";

for (int i = 0; i < PlayInfo.Instance.gametablelist.Count; i++)
{
dealer_img += PlayInfo.Instance.gametablelist[i].dlrimage;
dealer_img += ",";
}
Debug.Log("HERE ARE THE LINKS : " + dealer_img);

for (int j = 0; j < links.Length - 1; j++)
{

new BestHTTP.HTTPRequest(new System.Uri("*************************amazonaws.com/resources/" + "dealer/pic/" + links[j]),
(BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
=>
{
for (int k = 0; k < uitex.Length; k++) {
var tex = new Texture2D(20, 20);
uitex[k].GetComponent<UITexture>().mainTexture = tex;
}
}).Send();
}
}


The problem with this code is that it's only getting the last index of my links. What could be the problem on my iteration?

Here's an approach that works that shows what the expected behavior is:

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

new BestHTTP.HTTPRequest(new System.Uri("**************************.amazonaws.com/resources/"
(BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
=>
{
var tex = new Texture2D(20, 20);
uitex[0].GetComponent<UITexture>().mainTexture = tex;
}).Send();

new BestHTTP.HTTPRequest(new System.Uri("**************************.amazonaws.com/resources/"
(BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
=>
{
var tex = new Texture2D(20, 20);
uitex[1].GetComponent<UITexture>().mainTexture = tex;
}).Send();

new BestHTTP.HTTPRequest(new System.Uri("**************************.amazonaws.com/resources/"
(BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
=>
{
var tex = new Texture2D(20, 20);
uitex[2].GetComponent<UITexture>().mainTexture = tex;
}).Send();

new BestHTTP.HTTPRequest(new System.Uri("**************************.amazonaws.com/resources/"
(BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
=>
{
var tex = new Texture2D(20, 20);
uitex[3].GetComponent<UITexture>().mainTexture = tex;
}).Send();


It's working perfectly, but now the problem is that it is not very clean code.

• What's the expected behaviour? Currently you're iterating over all links, and once a download finishes, you're assigning the loaded image to all the UITextures, so obviously just one the content in link becomes visible. Are you for an example try to assign the image in link at index j to the texture at index j? Something else? – Tyyppi_77 May 7 '18 at 10:21
• edited it sir.. – TheGinxx009 May 7 '18 at 10:37

Since you want to assign a single link to a single texture, just capture the index into a lambda, kinda like you were doing.

for (int i = 0; i < newLinks.Length; 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/"
(BestHTTP.HTTPRequest req, BestHTTP.HTTPResponse res)
=>
{
var tex = new Texture2D(20, 20);

So we take the link at the index, and assign the downloaded image into the texture at the same index. The additional index variable is required, because C# captures by reference. See here.
• its strange sir . It's getting all the textures but the problem is that it has 1 error Array index is out of range. StackTrace on line 307 which is uitex[index].GetComponent<UITexture>().mainTexture = tex; – TheGinxx009 May 7 '18 at 10:56