I have an array (in a UnityScript file for Unity3D) which has checkpoints as GameObjects, and I am trying to write a function to swap them. So I have
public var cps: GameObject[]; //Initializes correctly
function swap(o1:GameObject,o2:GameObject)
{
var TempGO:GameObject = o1;
o1=o2;
o2=TempGO;
}
swap(cps[0],cps[1]); // nothing happens here.
Should I use pointers (not sure if they exist in js) or something else?
p.s.: I am actually trying to do a sort between them, based on their distance to a certain point, so any other suggestions would also be appreciated.
Thanks!
Javascript passes everything by value, you can't pass things by reference, and there are no pointers. Thus, if you want to swap two elements, you'll need to give it the array and the indices:
function swap(var arr:GameObject[], var i, var j) {
var temp:GameObject = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
and call it like
swap(cps, 0, 1);