ok there is probably a simple explanation for this…
but I haven’t been able to see it…
any ideas ?
I have a couple of arrays…
storing positions and variables, to play them back later…
but writing into one array seems to be affecting the other one…
The indices I’m using are not overflowing the array size…
what can be going on ? any ideas appreciated, thx…
this is how they are defined…
private var playbackPrev=new float [16]; //float data for previous stored info
private var playBackNext=new float [16]; //float data for next stored info
and here is one part of the data being read, but it appears to overwrite the other array… but how ?
var pos = playbackPrev[4];
Debug.Log(" POS:--------"+pos );
playBackNext[4] = parseFloat(str)/10;
pos = playbackPrev[4];
Debug.Log(" POS:--------"+pos );
the first Debug.Log and the second, print different things.
How is this possible ?
thanks
If you experienced ActionScript, you must know that in this language, when you make a variable other than boolean, int, string or float, assigning variables don’t give them values but a reference.
If we take an array, and we have two variables A and B, if we store the array in A, when asking A’s value, it will show us the array. If we do B = A, B takes the value or the reference of A. Because A contains a reference (to an array), it will take the reference.
So when you ask A’s value, or B’s value, you will get the same because B took A’s reference. Because they are pointing towards the same element, changing it either by A or B will, in the end, modify the same element : the array.
That’s also the fact for Unityscript. Have a look there.
But when I look at your code, what is the str variable ?
ahh that makes sense…
thanks
at a different time the code copies one array to the other… and its probably just copying the reference, so when updatng one array it is updating them both at the same time.
yeah I’m from c/c+= background… recently started messing with js c# didnt know the js vars worked as references like that.
I suppose I can just loop through all the elements to copy them individually, but is there a ‘proper’ way to copy the array without copying it as a reference ?
str is just a string read from an xml type string of info
thx again
In ActionScript, you had slice function.
For Unityscript, new Array (arrayToCopy) should do it.
var arr1 : Array = [0, 1];
var arr2 : Array = new Array (arr1);
function Start () {
arr1.Push (2);
// Should show [0,1]
Debug.Log (arr2);
}