Strange behaviour after creating array of Blocks

I’m trying to create a wall of cubes and then shift their reference in the array and update their position,
but when I tried to create a class to the cubes and inside of it a reference to a gameobject , everything goes wrong

public class Block  
{
    public GameObject obj;
   
    public Block()
    {
        
    }
	
}

Public class BlocksManager : MonoBehaviour
{
    //global var
    Block[,] array;
    Object[] textures;  
    // Reference to another scrips
    public static BlocksManager instance;
    //the block it self
    Block block;

    void Awake()
    {
        instance = this;
        block = new Block();
    }

void Start()

   {
     // reference to the cubes
     //left space to create more after

       array = new Block[9, 9]; 
    //Load imagens of the especific patch
    textures = Resources.LoadAll("Textures");
  
    //create 8X8 Blocks
    for (int i = 0; i <= 7; i++)
    {
        for (int j = 0; j <= 7; j++)
        {
            array[i, j] = CreateCube(i, j);
            array[i, j].obj.name = "" + i + "," + j;
            
        }
    }

Block CreateCube(int i, int j)
    {

        float referenceX = -2.071273f;
        float referenceY = -2.273989f;
        float cubeWidth = 0.60f;
        float cubeHeight = 0.60f;
   
        int i_ = i;
        int j_ = j;

        block.obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        block.obj.transform.position = new Vector3(referenceX + j_ * cubeWidth, referenceY + i_ * cubeHeight, 0);
        block.obj.transform.localScale = new Vector3(0.60f, 0.60f, 0.60f);
        block.obj.renderer.material = new Material(Shader.Find("Transparent/Diffuse"));
        block.obj.renderer.material.mainTexture = textures[0] as Texture;

        return block;

    }

public void ChangeState(Collision colission)
{

   //get the reference of the object
    //string[] array = colission.gameObject.name.Split(',');
   
    colission.gameObject.renderer.enabled = false;
    colission.gameObject.collider.enabled = false;
    MoveUpwards();
   
}
public void ChangeState(Collision colission)
    {
        colission.gameObject.renderer.enabled = false;
        colission.gameObject.collider.enabled = false;
        MoveUpwards();
    }

Here’s the problem, when this is executed accours a problem with the blocks, when I used a array of GameObjects instead a array of Block everything works fine

public void MoveUpwards()
{

    for (int i = 7; i >= 0; i--)
    {
        for (int j = 7; j >= 0; j--)
        {
            //move the blocks upwards
            float positionX = array[i, j].obj.transform.position.x;
            float positionY = array[i, j].obj.transform.position.y;
            print("I" + positionX);
            print("J" + positionY);

            array[i, j].obj.transform.position = new Vector2(positionX, positionY + 0.99f * 0.60f);
            //shift the reference
            array[i + 1, j] = array[i, j];
        }
    }

}

You only have a single object “block” in your whole manager. You initialize that in Awake(), but after that you just keep reassigning to block.obj of that one object. Note that CreateCube() doesn’t actually create an object of type Block, it just returns a reference to your one BlockManager.block.

In the end, your whole array is filled with references to that same object, so there also is only one value block.obj, and it points to the last object you created in the last call to CreateCube, i.e. the object for CreateCube(7,7).

Solution: Move this line:

block = new Block();

from Awake() to the beginning of CreateCube(). Also move the member variable block (line 20) into CreateCube as well.

Here the code:

public class Esfarelante : MonoBehaviour
{

    bool stepInsomething = false;
    Collision collision;


    // Update is called once per frame
    void Update()
    {

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Translate(new Vector3(-1, 0, 0) *     
            Time.deltaTime * 3.5f);
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Translate(new Vector3(1, 0, 0) *  
            Time.deltaTime * 3.5f);
        }

        if (Input.GetKeyUp((KeyCode.LeftControl)))
        {
            if (stepInsomething)
            {
               
		  BlocksManager.instance.ChangeState(this.collision);
               
            }
        }
    }
    void OnCollisionStay(Collision collision)
    {
        stepInsomething = true;
        this.collision = collision;
    }

    void OnCollisionExit(Collision collision)
    {
        stepInsomething = false;
    }
   
}