C# 2D Array of Class : why Class variables can't be changed?

Hy Everyone,

I get mad about a problem I have.
I’ve created a public Class called “BlockIDClass” with several variables.

using UnityEngine;
using System.Collections;
[System.Serializable]	

public class BlockIDClass {

	public enum Type {None, Path, Dirt}
	public Type type			= Type.None;
}

In another script, I’ve made a 2 dimensional array filled with BlockIDClass type object. I used a “for loop” to add the objects and each time I do this, I randomise an enumeration contained in the “BlockIDClass”, so they are not the same in the array.

using UnityEngine;
using System.Collections;

public class GridGenerator : MonoBehaviour {

	public int colNumber		= 10;
	public int rowNumber		= 10;
	public BlockIDClass [,] gridBlocksID	= new BlockIDClass [10,10];
	public BlockIDClass	gridBlockID;

	void Update ()
	{
		if (Input.GetKeyDown(KeyCode.Space))
		{
			generateGrid ();
		}
	}
	
	void generateGrid ()
	{
		for (int y=0; y < rowNumber; y++)
		{
			for (int x=0; x < colNumber; x++)
			{
				int randomTypeNumber 	= Random.Range(0, 3);
				gridBlockID.type		= (BlockIDClass.Type)randomTypeNumber;
				gridBlocksID [x,y] 		= gridBlockID;
				Debug.Log( "Enumeration type in for loop : "+gridBlocksID [x,y].type );
			}
		}
		foreach ( BlockIDClass i in gridBlocksID )
			{
			  Debug.Log( "Final Enumeration type : "+i.type );
			}
        }
}

When I Debug.log the enumeration type of each class object within the “for loop”, it appears that they indeed have a different type :

  • Enumeration type in for loop : None 36
  • Enumeration type in for loop : Path
    35
  • Enumeration type in for loop : Dirt
    29

But when I check after the for loop, all the enumeration type of each class object is the same :

  • Final Enumeration type : Dirt 100

If you know what is happening, it will be a great help, thanks !

Hey there, I think the problem is that you are setting variables on the same object, in your for loop where you have gridBlockID it is referencing the same object, so your array is just storing multiple references all to the same object. If you add gridBlockID = new BlockIDClass(); to the begining of your for loop it will be creating a new instance every iteration and putting this new object on the array that should solve your problem.

Ok, This works actually, this looks like this :

for (int y=0; y < rowNumber; y++)
       	{
	         for (int x=0; x < colNumber; x++)
	         {
	         	BlockID gridBlockID 	= new BlockID();
			int randomTypeNumber    = Random.Range(1, 3);
			gridBlockID.blocktype   = (BlockID.BlockType)randomTypeNumber;
		        gridBlocksID [x,y]      = gridBlockID;
	         }
      	}

I Just removed the gridBlockID declaration and put it in the for loop. Now each time that loops, a new instance of gridBlockID is created and put in the array.
As usual, it was just a little thing. :wink:
Thanks for the help, Phles.

Dont forget to turn your comment into an answer to let me vote it up.