# Array copy

**URL:** https://discussions.unity.com/t/array-copy/29108
**Category:** Questions & Answers
**Created:** [September 27, 2011, 5:03pm UTC](https://discussions.unity.com/t/array-copy/29108 "2011-09-27T17:03:19Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Tialkannos](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@Tialkannos](https://discussions.unity.com/u/Tialkannos)
#### Post date: [September 27, 2011, 5:03pm UTC](https://discussions.unity.com/t/array-copy/29108/1 "2011-09-27T17:03:19Z")

</div>

If I assign an array to another, both always keeps the same values.  
So what is the best way to copy the values from one array to another and still keep the arrays independent?  
(I know I can assign each element separately, but would this be the most efficient way?)

```
var first : int[] = new int[1];
var second : int[] = new int[1];

function Start ()
{
	first[0] = 1;
	second[0] = 2;
	Debug.Log("first:"+first[0]+" second:"+second[0]);
	//first:1 second:2
	first = second;
	Debug.Log("first:"+first[0]+" second:"+second[0]);
	//first:2 second:2
	first[0] = 1;
	Debug.Log("first:"+first[0]+" second:"+second[0]);
	//first:1 second:1
	
	//wish I could change the value of the first without affecting the second
}

```

---

<div class="post-metadata">

### Author: ![Tialkannos](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@Tialkannos](https://discussions.unity.com/u/Tialkannos)
#### Post date: [September 28, 2011, 3:13pm UTC](https://discussions.unity.com/t/array-copy/29108/3 "2011-09-28T15:13:15Z")

</div>

instead of

first = second;

the best way I could find is

System.Array.Copy(second,first,1);

---

<div class="post-metadata">

### Author: ![system](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/system/32/97283_2.png) [@system](https://discussions.unity.com/u/system)
#### Post date: [September 27, 2011, 5:22pm UTC](https://discussions.unity.com/t/array-copy/29108/2 "2011-09-27T17:22:55Z")

</div>

You can simply do

```
first=second;

```

That way it copies all of the properties of the second array into the first one , including length.

Another redundant way would be a for loop:

```
var forPointer:int=0;

for(;forPointer<first.length;forPointer++){
first[forPointer=second[forPointer];
}

```

This would return an error if the first had a bigger length than the second one.
