- Home /
how to initialize multidimensional jagged array
hiyas my code looks like:
public int[,,][] data;
public int worldX=16;
public int worldY=16;
public int worldZ=16;
void Start () {
[...]
int[,,][] data = new int[worldX,worldY,worldZ][];
for (int x=0; x<worldX; x++){
for (int z=0; z<worldZ; z++){
for (int y=0; y<worldY; y++){
if(y<=8){ data[x,y,z][0]=1; }
}
}
}
[...]
}
as you can see the first is the world position, I store in the second 10 Integer (int[9]) to save Variables for this point.
But unfortunately I can not get them to initialize in an elegant manner, is the only way to fill every slot (16x16x16x10) with zeros at start?
Answer by Em3rgency · Oct 25, 2014 at 11:15 PM
Short answer: Yes, that is the only way if you use simple arrays.
Sligtly longer answer: What you want are dynamic arrays. Arrays that can be expanded in size during runtime, as needed. I'd recommend looking into Lists.
Thank you but after a glass of whisky I decided to not like jagged arrays and created a custom class.
public class worldData
{
public int textureTop { get; set; }
public int texture$$anonymous$$id { get; set; }
public int textureBot { get; set; }
public int rot_x { get; set; }
public int rot_y { get; set; }
public int rot_z { get; set; }
public int shape { get; set; }
}
public worldData[,,] myData;
public int worldX=16;
public int worldY=16;
public int worldZ=16;
void Start () {
myData = new worldData[worldX, worldY, worldZ];
[...]
and so on :)
Your answer
Follow this Question
Related Questions
Assign gameobject value from an array (C#) 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity 3 GD HotShot-Tutorial Problem with an C#-Array 1 Answer
Convert To Using Array 2 Answers