title: Unity C# Array - how to clone
body: <p>Im developing a small app/game in Unity3D. </p>
  
  <p>The problem is: 
  I need to clone an array (call it tempArray) and make some modifications to it. Then i need to change values of the MAIN array to modified tempArray. However, whenever i make changes to cloned array, the same changes are made to the main one.</p>
  
  <p>So i used the following code:</p>
  
  <pre><code>private Cell[,] allCells = new Cell[256, 256];
  private Cell[,] cellClone = new Cell[256,256];
  
  //meanwhile initiated to some values//
  
  //Here i clone the array.
  cellClone = (Cell[,])allCells.Clone();
  
  //Here i output the values for an element from both arrays.
  Debug.Log(cellClone[0, 0].region.name.ToString());
  Debug.Log(allCells[0, 0].region.name.ToString());
  
  //Here i want to change "Region" variable of cellClone ONLY.
  cellClone[0, 0].setRegion(new Region("testregion123", Color.cyan, false));
  
  //Finally, i output the same values again. Only cellClone should change.
  Debug.Log(cellClone[0, 0].region.name.ToString());
  Debug.Log(allCells[0, 0].region.name.ToString());
  </code></pre>
  
  <p>However, the output shows that allCells[0,0] element was also changed. This means that any operation I do to the cloned array, is executed to the main array.</p>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>After alot of playing around I implemented this as a solution. Im posting this in case anybody has a similar problem.</p>
  
  <p>But Im not sure if this is how its supposed to be done so if anybody has any information - Ill be checking this post.</p>
  
  <pre><code>for (int i = 0; i &lt; allCells.GetLength(0); i++)
  {
      for (int j = 0; j &lt; allCells.GetLength(1); j++)
      {
          //cellClone[i, j] = allCells[i, j].Clone();
          //cellClone[i, j] = new Cell((int)allCells[i, j].position.x, (int)allCells[i, j].position.y, allCells[i, j].getRegionName());
          cellClone[i, j] = allCells[i, j].clone();
      }
  }
  </code></pre>
  
  <p>And the clone function:</p>
  
  <pre><code>public Cell clone()
  {
          Cell n = new Cell((int)position.x, (int)position.y, regionName);
          return n;
  }
  </code></pre>