- Home /
Setting arrays equal to each other issue
Hi,
when I want to make tempArr = masterArray, it works. However, when I change the contents of tempArr, the masterArray follows suit.
In my program I want the master array to be the unchanging list of data for the temp array to refer too. In my program I have this:
for (var i : int = 0; i<masterArray.length; i++){
tempInt = Mathf.Round(Random.Range(0, tempPosArray.length)); //random index
tempArrayP[i] = tempPosArray[tempInt];
tempPosArray.RemoveAt(tempInt);
}
tempPosArray = masterArray;
This function makes a new array(tempArrayP) which is the same thing as tempPosArray with a randomized order. In order to make this happen, I pick a random element out of the array, I plug it into the new temp array and remove it from the old one So that there are no duplicates.
The problem: when I remove elements from tempPosArray, those same elements are moved from masterArray as well...and WHY? masterArray is on the left side of the equal sign...I just don't get it. is there a way to make masterArray concrete, unchanging. masterArray is set in the Start (); function.
You are sure there is no place in you code that says something like
masterArray=oneOfTheOtherArrays;
Since that will modify the masterArray ins$$anonymous$$d of the other array. You said "masterArray is on the left side of the equal sign" which will modify the masterArray and not the other, $$anonymous$$d posting the other lines where masterArray is compared to another array.
oops, I meant 'right side' as shown above where 'tempPosArray = masterArray;'
this function is called through a Send$$anonymous$$essage on startup: function GetOriginalPosition (theirRelativePosition : Vector3){ masterArray[iP] = theirRelativePosition; iP++; }
This function is called right before the for loop in my OP. function RefreshValues () { tempPosArray = masterPositionArray; sendIndex = 0;
}
the first function is where masterArray is set, the second function is where it is used(plugged into temp array)
oh by the way, masterArray is the same thing as masterPositionArray
If you are doing the above just to randomize the array, a simpler method is to shuffle the array in place. Just repeatedly swap randomly selected elements a bunch of times.
Answer by GamesRUs · May 23, 2012 at 06:59 PM
Using the assignment operator, you are actually just referencing the original Array. To make a copy, instantiate the new array with the same size as the source array, then iterate through the original array and assign the individual values to the same index within the new array.
Answer by hathol · May 23, 2012 at 06:53 PM
You have to understand that arrays are reference types. That means, that they don't actually store values, they only store references to those values. So when you have a line of code like
tempPosArray = masterArray; // called before your function as well I suppose
what you actually do is copying a reference to a memory location, meaning that any changes to the memory at that location (including removing elements) will be reflected in both arrays. That was the "why" part. Now to change this, just use
tempPosArray = Array.copy(masterArray, tempPosArray, masterArray.Length);
what is Array.copy();? I don't see it anywhere in the Unity API.
Array.Copy is a c# function that copies on array into another. http://msdn.microsoft.com/en-us/library/system.array.copy(v=vs.71).aspx
sry, I didn't realize you were using JS. Use the method @GamesRUs proposed ins$$anonymous$$d then :)
Yes, I started to say to use Array.Copy until I noticed it was JS. :)
it almost works, for me it said Assets/$$anonymous$$archingCubes3/@Scripts/tunnels.js(36,51): BCE0022: Cannot convert 'void' to 'float[]'.
distances_sort = System.Array.Copy(distances, distances_sort, nodes.Length);
You do not need the 'distanaces_sort = ' portion as the Array.Copy method does not return a value. What it does is takes the array in parameter one (distances) and copies it into the array in parameter two (distances_sort) starting with the first element and stopping with the element index specified in parameter three (nodes.Length in your case). Are you sure that you don't want to use distances.Length ins$$anonymous$$d of nodes.Length though?
In short - the error you received is telling you that Array.Copy returns void and you are trying to assign void to a variable of type float[] (an array of floats which is the distances_sort array).