- Home /
How to Set a Multidimensional Array Length
I have the following code:
public int lengthInt;
public int[, ] data = new int[0, 0];
void Start(){
data.Length(0) = lengthInt; //error
data.Length(1) = lengthInt; //error
}
From what I understand you can't just have:
public int lengthInt;
public int[, ] data = new int[lengthInt, lengthInt];
You have to define the array length in a runtime function. However if I just had:
public int lengthInt;
void Start(){
public int[, ] data = new int[lengthInt, lengthInt];
}
That would work but then it doesn't show up in the inspector. So how do I set a multidimensional array length and have it visible in the inspector?
Thanks!
Answer by Simon V · Jan 08, 2012 at 06:25 PM
The problem is that lengthInt == null
in your first try. If you assign a value to lengthInt
, before you define the array it will work.
Your first example should become:
public static int lengthInt = 0;
public int[, ] data = new int[lengthInt, lengthInt];
The static tag however, will make that your class can only have one instance.
And arrays are never visible in the inspector by the way.
Still doesn't work I get the following error: "A field initializer cannot reference the nonstatic field, method, or property..."
Ooh yes, completely forgot that. You have to add the static
tag to lengthInt
. Will update my answer.
Your answer

Follow this Question
Related Questions
2D array in shader 0 Answers
GameObjects in multidimensional Arrays 3 Answers
Multidimensional array trouble 2 Answers
Newbie Problem with Multidimensional Arrays in JavaScript 0 Answers
pragma strict create List of Array of strings for class? 1 Answer