Copy of an array?

Hi, for my pathFinding codes, I need something to copy an entire array and send it to another gameObject with the same code attached…

This is too complicated and too slow, so I need something different:

// exampleCode.js
var pos : Vector3[];
var playerObj : exampleCode.js
var num : int = 0;

function Update () {
 ...
 ...
 if (pos[pos.length] != Vector3.zero) {
  playerObj.pos[num] = pos[num];
  num ++;
 }
 if (num == pos.length) {
  Destroy(gameObject);
 }
}

I tried this, but I doesn’t seem to be working:

...
...
player.pos = pos;
...

If you have other methods to do that, please tell me :slight_smile:

Javascript gurus arrest me if I’m wrong, but I think you can use the slice() method.

playerPos.pos = pos.slice();

unitron doesn’t even know this word and it’s not signed in the scripting documentation… also, it’s not a member of Vector3.
Sorry, that’s not working :?

Two ways to go here. Either just use a loop:-

for (i = 0; i < pos.Length; i++) {
    playerObj.pos[i] = pos[i];
}

…or, if you’re lazy, you can use System.Array.Copy:-

System.Array.Copy(playerObj.pos, pos, pos.Length);

I’m lazy, so I’d recommend you use the second method :wink:

Thanks, but I did simply use:

player.pos = pos;

I discovered that due to a mistake, it wasn’t working the first time… :slight_smile: