Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Update a grid with a 3D rectangle

Discussion in 'Scripting' started by julien83franceschi, Jan 31, 2024.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    243
    Hello everyone,

    I'd like to update the array of a grid (int [,] Grid = new int [8, 8]) .

    The grid must put 1's where the 3D rectangle is dropped (this one is composed of 5 horizontal cubes).

    And it must set 0s where it was when removed from the grid.

    I use the following code:

    Code (CSharp):
    1. public class grilleRouge : MonoBehaviour
    2. {
    3.     private int clickedRow;
    4.     private int clickedCol;
    5.     int[,] Grille = new int[8, 8];
    6.     void Start()
    7.     {
    8.         clickedRow = 1;
    9.         clickedCol = 1;
    10. //       CreateGrid();
    11.     }
    12.  
    13.     int[,] pentaminoMatrix = new int[,]
    14.     {
    15.     {1, 1, 1, 1, 1},
    16.     {0, 0, 0, 0, 0},
    17.     {0, 0, 0, 0, 0},
    18.     {0, 0, 0, 0, 0},
    19.     {0, 0, 0, 0, 0},
    20.     };
    21.  
    22.     void Update()
    23.     {
    24.         if (Input.GetMouseButtonUp(0))
    25.         {
    26.  
    27.             foreach (Transform child in transform)
    28.             {
    29.                 bool hasHit2 = (Physics.Raycast(child.position, Vector3.down, out hit, Mathf.Infinity));
    30.                 if (hasHit2 == true)
    31.                 {
    32.                     Debug.DrawRay(child.transform.position, Vector3.down * 10, Color.red);
    33.                 }
    34.                 else
    35.                 {
    36.                     Debug.DrawRay(child.transform.position, Vector3.down * 10, Color.green);
    37.                  }
    38.             }
    39.  
    40.             PrintArrayValues(Grille);
    41.             PlacePentaminoInGrid(clickedRow, clickedCol, pentaminoMatrix);
    42.         }
    43.  
    44.  
    45.         bool IsWithinGrid(int row, int col)
    46.         {
    47.             return row >= 0 && row < Grille.GetLength(0) && col >= 0 && col < Grille.GetLength(1);
    48.         }
    49.  
    50.         int PlacePentaminoInGrid(int clickedRow, int clickedCol, int[,] pentaminoMatrix)
    51.         {
    52.  
    53.             for (int row = 0; row < pentaminoMatrix.GetLength(0); row++)
    54.             {
    55.                 for (int col = 0; col < pentaminoMatrix.GetLength(1); col++)
    56.                 {
    57.                     int targetRow = clickedRow + row;
    58.                     int targetCol = clickedCol + col;
    59.  
    60.  
    61.                     if (IsWithinGrid(targetRow, targetCol))
    62.                     {
    63.                         if (Grille[targetRow, targetCol] == 1)
    64.                         {
    65.                             return 1;
    66.                         }
    67.  
    68.                         Grille[targetRow, targetCol] = pentaminoMatrix[row, col];
    69.  
    70.                         Debug.Log("Le rayon touche l'objet : " + hit.transform.name);
    71.  
    72.                         PrintArrayValues(Grille);
    73.                     }
    74.                 }
    75.             }
    76.  
    77.             return 2;
    78.         }
    79.     }
    80.  
    81.     void PrintArrayValues(int[,] array)
    82.     {
    83.         for (int i = 0; i < array.GetLength(0); i++)
    84.         {
    85.             for (int j = 0; j < array.GetLength(1); j++)
    86.             {
    87.                 Debug.Log("Grille[" + i + "," + j + "] = " + array[i, j]);
    88.             }
    89.         }
    90.     }
    91. }
    Here is a screenshot:



    My problem is that the console doesn't update the Grid table properly (with 0 and 1 values).

    If you can review the code,
    or get the Debug.Log working

    Thank you for your help,

    A+
     
    Last edited: Jan 31, 2024