Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Make a dynamic array?

Discussion in 'Scripting' started by Faestus, Aug 6, 2014.

  1. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Hi. I need to make a dynamic array in Csharp. This is what I have:

    Code (CSharp):
    1. staticmap = new int[gridFloors]{};
    2.         for(zz = 0; zz < gridFloors; zz++) {              
    3.             staticmap[zz] = new int[gridWidth]{};
    4.             for(xx = 0; xx < gridWidth; xx++) {              
    5.                 staticmap[zz][xx] = new int[gridHeight]{};
    6.                 for(yy = 0; yy < gridHeight; yy++) {
    7.                     staticmap[zz][xx][yy]=0;
    8.                 }              
    9.             }
    10.         }
    This returns me "Array index is out of range"...
     
  2. Medding3000

    Medding3000

    Joined:
    Dec 11, 2013
    Posts:
    45
    You want a dynamic 3dimensional array if i understand correct?
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Your first line needs to read
    staticmap = new int[gridFloors][][];
     
  4. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    I get "A constant value is expected" error...
     
  5. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    if u want resizeable array at runtime use list.

    Code (CSharp):
    1. List<int> list = new List<int>();
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    I assumed that gridFloors was a value, as you used it yourself in your example.

    I also should have pointed out that line 3 should be:
    taticmap[zz] = new int[gridWidth][];

    And the staticMap should be of type int[][][].

    ps. Note there is no { } in my code.