Modifiying One Value in an Array Modifies All Values

In a 2D array of objects, whenever I modify a single value in the array, all values are also then modified in the same way. I have been struggling with this same problem for a while now and am looking for an explanation as to why things are behaving this way.

using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using System.Text;


public class C_Generate_Level : MonoBehaviour {

	//enum Cardinal {Top, Right, Bottom, Left};

	T[,] InitializeArray<T>(int height, int width) where T : new()
	{
		T[,] array = new T[height, width];
		for (int i = 0; i < height; ++i)
		{
			for (int g = 0; g < width; ++g)
			{
				array[i, g] = new T();
			}
		}
		
		return array;
	}

	class example 
	{
		public void initialize_order ()
		{
			first = 1;
			second = 2;
		}

		public void get_order ()
		{
			print (first + " " + second);
		}

		public void reverse_order ()
		{
			int hold;

			hold = first;
			first = second;
			second = hold;
		}
		
		int first;
		int second;
	}
	

void Start ()
{
		const int DIMENSIONS = 2;

		int xCounter;
		int yCounter;
		int display_counter = 1;

		example[,] exampleAry = InitializeArray<example>(DIMENSIONS, DIMENSIONS);

		example template = new example ();
		template.initialize_order ();

		for (yCounter = 0; yCounter < DIMENSIONS; yCounter++)
		{
			for (xCounter = 0; xCounter < DIMENSIONS; xCounter++)
			{
				exampleAry[yCounter, xCounter] = template;
			}

		}
				
		for (yCounter = 0; yCounter < DIMENSIONS; yCounter++)
		{
			for (xCounter = 0; xCounter < DIMENSIONS; xCounter++)
			{
				print (display_counter);
				exampleAry[0,0].get_order();
				exampleAry[0,1].get_order();
				exampleAry[1,0].get_order();
				exampleAry[1,1].get_order();
    
    				if (1 == xCounter && 0 == yCounter)
    				{  /////
    					exampleAry[yCounter, xCounter].reverse_order(); //This line is what confuses me: it performs reverse_order on every value in exampleAry, instead of only the current value
    				}  /////
    				print (" ");
    
    				display_counter++;
    			}
    		}

		print (display_counter);
		exampleAry[0,0].get_order();
		exampleAry[0,1].get_order();
		exampleAry[1,0].get_order();
		exampleAry[1,1].get_order();

		
    		return;
}

The output is as follows:

1
1 2
1 2
1 2
1 2

2
1 2
1 2
1 2
1 2

3
2 1
2 1
2 1
2 1

4
2 1
2 1
2 1
2 1

5
2 1
2 1
2 1
2 1

I think you may be mixing C# with C++ in how assignment is handled.

example[,] exampleAry = InitializeArray<example>(DIMENSIONS, DIMENSIONS);

Here, you are creating a new array and assigning each value a unique ‘example’

         example template = new example ();
         template.initialize_order ();
 
         for (yCounter = 0; yCounter < DIMENSIONS; yCounter++)
         {
             for (xCounter = 0; xCounter < DIMENSIONS; xCounter++)
             {
                 exampleAry[yCounter, xCounter] = template;
             }
         }

Here, you are creating a single example instance, then going through each element of exampleAry and replacing the reference to your original example references you created in InitializeArray, with the example reference you created and assigned to ‘template’. The end result is that each and every element of exampleAry points to the exact same example instance, the one you called ‘template’. Any changes you make to a single element in exampleAry is going to appear to change all elements, as every position in the array points to the exact same example instance.

I am guessing that you intended to copy the value of template to each element of the array, instead of replace the instance altogether, which is pretty much what would happen in C/C++, in which case:

          for (yCounter = 0; yCounter < DIMENSIONS; yCounter++)
          {
              for (xCounter = 0; xCounter < DIMENSIONS; xCounter++)
              {
                  example template = new example ();
                  template.initialize_order ();
                  exampleAry[yCounter, xCounter] = template;
              }
          }

Should work, though keep in mind that your InitializeArray function is now creating a bunch of redundant ‘example’ instances that wont ever do anything except get garbage collected.

Alternatively, try turning your ‘example’ class into a struct, which is a value type that will behave similarly to how C/C++ would handle assignment. I don’t know how that will work with your generic function ‘InitializeArray’ though.