0

EDITED :

Hello, I have a grid

Vector2[][] grid;

I would like to get those value

enter image description here

Like this in a dictionary

Vector2.up => [[0][1],[0][2],[0][3],[0][4],[0][5]]
Vector2.left=> [[1][0],[2][0],[3][0],[4][0],[5][0]]
Vector2.right=> [[1][6],[2][6],[3][6],[4][6],[5][6]]
Vector2.down=> [[6][1],[6][2],[6][3],[6][4],[6][5]]

I am doing this :

void GetSpawnablePosition() {
        Vector2[] coordX = { Vector2.up, Vector2.down };
        Vector2[] coordY = { Vector2.left, Vector2.right };
        for (int i = 0; i < coordX.Length; i++)
        {
            Vector2[] newArray = new Vector2[enemyGrid.grid[0].Length - 2];
            if (coordX[i] == Vector2.up)
                System.Array.Copy(enemyGrid.grid[0], 1, newArray, 0, enemyGrid.grid[0].Length - 2);

            if (coordX[i] == Vector2.down)
                System.Array.Copy(enemyGrid.grid[enemyGrid.grid[0].Length - 1], 1, newArray, 0, enemyGrid.grid[0].Length - 2);

            spawnablePosition.Add(coordX[i], newArray);
        }
        for (int i = 0; i < coordY.Length; i++)
        {
            Vector2[] newArray = new Vector2[enemyGrid.grid.Length - 2];

            if (coordY[i] == Vector2.right)
                System.Array.Copy(enemyGrid.grid, 1, newArray, 0, enemyGrid.grid.Length - 2);

            if (coordY[i] == Vector2.left)
                System.Array.Copy(enemyGrid.grid, enemyGrid.grid.Length - 1, newArray, 0, enemyGrid.grid.Length - 2);

            spawnablePosition.Add(coordY[i], newArray);
        }

        Debug.Log(spawnablePosition);
    }

But I got ArgumentException: length, I know my second for is probably broken, I am trying to find a good way to do it... but I always failed for the moment

Isn't there an easiest way to accomplish my task, cause really, I am confused a lot, I saw about Linq but apparently unity can't use it ? (I added Using Systen.Linq) but when i do array.Where() it say error.

Community
  • 1
  • 1
Bobby
  • 4,372
  • 8
  • 47
  • 103
  • Is memory/efficiency a factor? If not, you could loop through like you describe and just create a new array, copying the elements. If you are doing this with a very large array or substantially sized objects, etc you could copy the references into a new array to avoid copying the data itself (if pointers). Could you explain a little bit more about what you tried (specifically the loop) and why you can't get the first or last row? – Isaac Apr 17 '18 at 03:48
  • Well, I found a way, but i changed the array from Vector2[,] to Vector2[][] What is actually the difference ? – Bobby Apr 17 '18 at 03:49
  • Here is a link to where that question has been asked:https://stackoverflow.com/q/12567329/6724120 – Isaac Apr 17 '18 at 03:57
  • Thanks, so it is way easier to manipulate jagged array right? The array I want to make is uniform, but the problem in "why you can't get the first or last row?" I don't know how to navigate correctly in a 2d array. If I want to get all the first row of the jagged array for exemple, what is the syntax ? I checked this, it looks so complicated where jagged array are so easy to understand https://stackoverflow.com/questions/27427527/how-to-get-a-complete-row-or-column-from-2d-array-in-c-sharp – Bobby Apr 17 '18 at 04:01

0 Answers0