JS. How to make clone of array?

var firstArray : Array = new Array(0,1,2,3,4,5);
var secondArray : Array;

secondArray = firstArray;

Creates an additional reference to the array.

var firstArray : Array = new Array(0,1,2,3,4,5);
var secondArray : Array;

System.Array.Copy(firstArray, secondArray, firstArray.length);

No appropriate version of ‘System.Array.Copy’ for the argument list ‘(Array, Array, int)’ was found.

How to use System.Array? Where can I read about it? How to clone array?

I don't use Javascript but I think Array.Copy only accepts bracket arrays (int[], etc.). It works if you declare your array like this: var fArray : int[] = [0,1,2,3,4,5]; var sArray : int[]; function Start () { //Receiving array must be resized first sArray = new int[fArray.length]; System.Array.Copy(fArray, sArray, fArray.length); } If you really want to use 'Array' type then you can manually iterate through the array and set each value.

Thank you. It work, but now is too much Array's in code.

1 Answer

1

Looks like this would work, although I have never used it: Array.slice

http://www.w3schools.com/jsref/jsref_slice_array.asp

var secondArray : Array = firstArray.slice (0, firstArray.length);

Try that. I should note unityAnswers is crammed full of questions with responses yelling at them to use Lists in java script instead of Array.

Thank you. I knew about concat() and slice() in java script, but nothing worked this morning. I thought here is something else in UnityScript. My fault