I’m about to go crazy. Trying to store values in a list of Vector2, and it’s just not working. Here’s a script demonstrating the issue:
function Test()
{
var arr = new float[100];
for (val in arr)
{
val = Random.value;
}
var tempMaxList = List.<Vector2>();
tempMaxList.Add(Vector2.zero);
for(var i:int=0;i<arr.length;i++)
{
if(arr*>tempMaxList[0].y)*
{ tempMaxList[0].x = i; tempMaxList[0].y = arr*;* Debug.Log(tempMaxList[0]+" “+i+” "+arr*); //tempMaxList[0] is 0,0, WTF???* } } } If I replace tempMaxList by a simple Vector2 var, everything is fine. I’m sure epic face palming will follow, but I just can’t see what I’m doing wrong here…
var arr = new float[100];
for (val in arr)
{
val = Random.value;
}
I don’t use UnityScript, but a foreach / for-in loop uses an IEnumerator to iterate through the array. You can never assign values to the iterator variable since it’s always just a copy of the value stored in the array. Use a normal for loop:
var arr = new float[100];
for (var n = 0; n< arr.Length; n++)
{
arr[n] = Random.value;
}
Second, your problem that the returned values are 0,0 is that Vector2 is a value type. When you use the indexer of your List you get a copy of the item. Changing the members of this copy won’t change the item in the list.
{ tempMaxList[0] = new Vector2(i,arr*);* } } or for(var i:int=0;i<arr.length;i++) { if(arr*>tempMaxList[0].y)* { var tmp = tempMaxList[0]; tmp.x = i; tmp.y = arr*;* tempMaxList[0] = tmp; } } Lastly, I don’t get why you use a List. You just add one item and always use this like a single variable. So the whole thing could be done like this: function Test() { var arr = new float[100]; for (var n = 0; n< arr.Length; n++) { arr[n] = Random.value; }