I came across this puzzling behavior while making a simulation project in Unity3D.
Basically, I was creating anonymous functions in a loop using the variable being looped, and add all these functions to a queue for processing.
The curious case in Unity3D Monobehavior is that they are only called on the last looped variable.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class TestDist : MonoBehaviour {
int counter = 0;
// Use this for initialization
void Start () {
List<LOL> lols = new List<LOL>();
lols.Add(new LOL(1));
lols.Add(new LOL(2));
lols.Add(new LOL(3));
List<Func<int>> fs = new List<Func<int>>();
foreach (LOL l in lols)
{
//****** Critical Section *********
Func<int> func = () =>
{
Debug.Log(l.ID); //Prints "3" 3 times instead of
return 0; // "1", "2", "3"
};
//****** Critical Section *********
fs.Add(func);
}
foreach (Func<int> f in fs)
{
f();
}
}
// Update is called once per frame
void Update () {
}
}
class LOL
{
public long ID;
public LOL(long id)
{
ID = id;
}
}
The code works good in plain Visual Studio Console application, but fails on Unity3D. (Version 5.0) by printing the last value ("3") 3 times instead of 1,2,3.
I tried various methods to circumvent and the following worked for no apparent reason :
Changing the Critical Section to the following block solves the issue:
LOL another = l;
Func<int> func = () =>
{
Debug.Log(another.ID);
return 0;
};
Will be delighted if someone can answer (Unity Bug Fix Team does not seem to care)