Hi everyone,
I’m having some trouble getting a (seemingly) simple piece of code to work:
List<Bar[]> rows = new List<Bar[]>();
float x = 0;
float y = 0;
for (int i = 0; i < rows.Count; i++)
{
Bar[] b = rows[i];
if (b.Length == 0)
continue;
foreach (Bar b1 in b)
{
float cx = x; //create local copies of this variable to prevent closure issues - this fails?
float cy = y;
b1.Position = new Vector2(cx, cy);
x += b1.Size.x;
Debug.Log("Pos1: " + b1.Position);
}
foreach (Bar b1 in b)
{
Debug.Log("Pos2: " + b1.Position);
}
x = 0;
y += b[0].Size.y;
}
Attached to this post you can find a screenshot of the console output. You’d expect getting the same results for Pos1 as for Pos2, but it seems that only the last value of Pos1 is kept… I’m converting an XNA project to Unity and in XNA this code works just fine, even without the cx and cy variables.
I’ve been looking at this for more than 3 hours now and I’m probably missing something very stupid. This is getting really frustrating :?
I’d appreciate it if you can take a look at it and thanks in advance!
Francois