If I assign an array to another, both always keeps the same values.
So what is the best way to copy the values from one array to another and still keep the arrays independent?
(I know I can assign each element separately, but would this be the most efficient way?)
var first : int[] = new int[1];
var second : int[] = new int[1];
function Start ()
{
first[0] = 1;
second[0] = 2;
Debug.Log("first:"+first[0]+" second:"+second[0]);
//first:1 second:2
first = second;
Debug.Log("first:"+first[0]+" second:"+second[0]);
//first:2 second:2
first[0] = 1;
Debug.Log("first:"+first[0]+" second:"+second[0]);
//first:1 second:1
//wish I could change the value of the first without affecting the second
}
My problem is that when I put first = second; the two arrays always keeps the same values, so when change one, the other also changes with the for loop I can do what I want, but my question is if it is the best way, because my arrays are quite large but thanks for your answer anyway
– TialkannosIn my code I put this line first[0] = 1; but this change the value in the second array too, I think it is because the arrays now have the same reference (I tried to explain with the comments) I just want to have a copy of an array, so I can compare if one value have changed. But in this way, both always have the same values, then it is useless to compare.
– Tialkannos