user4589444
user4589444

Reputation:

Action callback conflicting with for loop

I have strange problem and I need help.

When I'm printing loop variable i, It is printing i = 3. However it must be 0, 1 or 2.

This is my code:

IEnumerator Start()

{

itemCount = 3;

for (int i = 0; i < itemCount; i++)
{
   StartCoroutine(TextureFromURL(textureURLString[i], (Sprite callback) =>
   {
      print("i = " + i);
      AddItem();
      GameObject.Find("pashaItem(Clone)").GetComponentInChildren<Text>().text = descriptions[u];                    
      GameObject.Find("pashaItem(Clone)").transform.GetChild(1).GetComponent<Image>().sprite = callback;
      GameObject.Find("pashaItem(Clone)").name = "pashaItem" + (u + 1).ToString();
    }));
        }
 }

    IEnumerator TextureFromURL(string url, Action<Sprite> callback)
    {
        Sprite s = new Sprite();
        WWW textureWWW = new WWW(url);
        yield return textureWWW;

        if (!String.IsNullOrEmpty(textureWWW.error))
        {
            print("null ve ya bos");
        }
        else
        {
            Texture2D myTexture = textureWWW.texture;
            s = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0.5f, 0.5f));
        }

        callback(s);
    }

Upvotes: 2

Views: 239

Answers (2)

Mitklantekutli
Mitklantekutli

Reputation: 410

It happens of reason how mechanics of captured variables works. In two words - for the first your code makes variable 'i' to be 3 and only after your code starts corutine. Decision for this issue - just add variable that stores value of 'i' inside callback :)

var local_i = i;
print("local_i = " + local_i);

You can read more here: http://blogs.msdn.com/b/matt/archive/2008/03/01/understanding-variable-capturing-in-c.aspx

Upvotes: 0

Marius Junak
Marius Junak

Reputation: 1213

The problem is the anonmyous method. They are refering to the same loop variable i. You can solve it by making a copy of "i".

I stripped down your code to make a simple working Example.

 void Start()
{
    var itemCount = 3;

    for (int i = 0; i < itemCount; i++)
    {
        var x = i; // Important Line
        StartCoroutine(TextureFromURL(() =>
        {

            print("i = " + x);
        }));
    }
}

IEnumerator TextureFromURL( Action callback)
{
    yield return null;
    callback();
}

Upvotes: 1

Related Questions