ChihWei Huang

ChihWei Huang

  • NA
  • 6
  • 563

List Matrix to Array Matrix

Jul 2 2021 2:56 AM

I am trying to recreate tetris in the Unity game engine. I want to create a grid (the matrix) but also set the size depending on in-game variables. Since arrays need constant variables, other websites recommended that I make a list and use the list.ToArray() function, but it does not work for matricies. 

 

private int[,] tetrisGrid;

// inside Start function

        List<List<int>> tetrisGridConverting = new List<List<int>>();
        
        for (int y = 0; y < yLength; y ++)
        {
            List<int> yTemp = new List<int>();

            for (int x = 0; x < xLength; x ++)
            {
                yTemp.Add(0);
            }
            tetrisGridConverting.Add(yTemp);
        }

        Debug.Log(tetrisGridConverting);
        tetrisGrid = tetrisGridConverting.ToArray(); //  cannot implicitly convert List<int>[]  to  int[*,*]


Answers (1)