Search Unity

Declaring and Initializing 2D arrays in C#

Discussion in 'Scripting' started by jaycee09, Jun 30, 2010.

  1. jaycee09

    jaycee09

    Joined:
    Jun 24, 2010
    Posts:
    26
    Can anyone tell me what's the best way to initialize a 2D array of vectors in C#??

    This is what I tried to do:

    Code (csharp):
    1. private Vector3[][] spawnGrid;
    2.  
    3.  
    4. spawnGrid[0][0] = new Vector3(-2.5f, 0f, 1.5f);
    5.         spawnGrid[0][1] = new Vector3(-2.5f, 0f, 0.5f);
    6.         spawnGrid[0][2] = new Vector3(-2.5f, 0f, -1.5f);
    7.         spawnGrid[0][3] = new Vector3(-2.5f, 0f, -2.5f);
    8.  
    9.         spawnGrid[1][0] = new Vector3(-1.5f, 0f, 1.5f);
    10.         spawnGrid[1][1] = new Vector3(-1.5f, 0f, 0.5f);
    11.         spawnGrid[1][2] = new Vector3(-1.5f, 0f, -1.5f);
    12.         spawnGrid[1][3] = new Vector3(-1.5f, 0f, -2.5f);
    13.  
    14.         spawnGrid[2][0] = new Vector3(0.5f, 0f, 1.5f);
    15.         spawnGrid[2][1] = new Vector3(0.5f, 0f, 0.5f);
    16.         spawnGrid[2][2] = new Vector3(0.5f, 0f, -1.5f);
    17.         spawnGrid[2][3] = new Vector3(0.5f, 0f, -2.5f);
    18.  
    I dont get any syntax error but I get a NullReferenceException at the point where I access an element of this array like so:

    Code (csharp):
    1. GameObject GO;
    2.             GO = (GameObject)Instantiate(enemies[0], spawnGrid[0][0], transform.rotation);
    3.            
    Does anyone see the problem here?

    Please help!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You haven't initialized any of the arrays. In any case, that's the syntax for a jagged array; seems like you want a 2D array.

    Code (csharp):
    1. private Vector3[,] spawnGrid = new Vector3[3, 4];
    --Eric
     
    Oranger97 and hms0589 like this.
  3. jaycee09

    jaycee09

    Joined:
    Jun 24, 2010
    Posts:
    26
    Thanks for replying. But this time, I am getting a new error like such:

    Code (csharp):
    1. error CS0022: Wrong number of indexes `1' inside [], expected `2' at the point I am accessing an element.
    Do you see the problem here?

    Thanks!
     
  4. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    If you intialize the array as eric proposed, you need to access elements like this:
    Code (csharp):
    1. spawnGrid[0,0] = new Vector3(-2.5f, 0f, 1.5f);
     
  5. jaycee09

    jaycee09

    Joined:
    Jun 24, 2010
    Posts:
    26
    Thanks a lot guys...I am not familiar with a lot of syntax in C# and I am still very new to this language. But thanks to you guys to make my life easier!
     
  6. dpw

    dpw

    Joined:
    May 20, 2014
    Posts:
    6
    You are probably done with this coding already, but you can make it even simpler just by doing this:


    1. private Vector3[,] spawnGrid = new Vector3[3, 4];

    2. void SpawnGrid()
    3. {
    4. for (int i = 0; i < 3; i++)
    5. {
    6. for (int j = 0; j < 4; j++)
    7. {
    8. spawnGrid[i, j] = new Vector3(x, 0f, z);
    9. z -= 1;
    10. }
    11. x += 1;
    12. z = 1.5f; //gives original value back to Z.
    13. }
    14. }