- Home /
Multidimensional Arrays of Classes in Java
I am attempting to create a simple Cube/Sauerbraten/Minecraft-style game as practice with using Unity. I can't work out how to create a multidimensional array of classes. The following code is what I'm using at the moment, but it just keeps getting a null reference exception:
public class CubeClass {
public var cubematerial : byte; //0=air, 1=solid
public var cubeobject : GameObject;
}
public var WorldSize : int=256;
var Cube : CubeClass[,,];
for(var z : int=0;z<WorldSize;z++) {
for(var y : int=0;y<WorldSize;y++) {
for(var x : int=0;x<WorldSize;x++) {
Cube[x,y,z]=new CubeClass();
}
}
}
Also, why can't I just type
for(int z=0;z<WorldSize;z++) {
I'm new to Java so I may be missing something but I read a tutorial on Java in general and it said you could do it that way, so why doesn't it work in Unity?
Thanks, David
Unity doesn't use Java. Also, you really don't want to use a separate GameObject for each cube, since 16.78 million GameObjects will crash Unity bigtime. (Generally you should use <10$$anonymous$$ objects, and can't go above 65$$anonymous$$.) Build a series of meshes ins$$anonymous$$d.
Ah, I hadn't actually done the maths. Thanks for pointing that out. I hadn't realised a world size of 256 would result in 16.78 million GameObjects! 256*256*256=16,777,216. That's possibly a few too many! As it happens, it did crash Unity (after I fixed the code). I had no idea why, though.
Now the problem is how to keep track of a series of meshes. I don't know Unity well enough to even know if a mesh has some kind of referencing system (maybe a reference type is returned when a mesh is created? I need to actually go read the manual, don't I?).
Answer by rutter · Apr 02, 2012 at 06:22 PM
In a nutshell, Java and JavaScript are not the same language.
Declaring an int in Java:
int x;
Declaring an int in UnityScript (Unity's custom implementation of JavaScript):
var x : int;
The meaning is more or less the same, but the syntax is different. That's why your for
loop looks different.
From there, you're declaring an array named Cube
, but not actually creating it. Before accessing the array, you'll need something like this:
Cube = new CubeClass[WorldSize, WorldSize, WorldSize];
If you access the array before then, you'll get a NullReferenceException.
Just for the benefit of everyone else, rutter was right in that "Cube = new CubeClass[WorldSize, WorldSize, WorldSize];" is exactly what I should have added to my code. The only problem I encountered after that was that 256*256*256=16,777,216. That 16.78 million GameObjects. Eric5h5 suggested that I use a series of meshes rather than GameObjects, which is what I will do.
Answer by Bunny83 · Apr 02, 2012 at 06:20 PM
First of all you have to actually create the array. I'm not sure if you can do this in UnityScript (JavaScript). I can remember that there was a problem with multidimensional array in UnityScript, but i'm not sure if they fixed it. In C# you would create the array like this:
// C#
CubeClass[,,] Cube; //variable declatarion
Cube = new CubeClass[WorldSize ,WorldSize ,WorldSize];
Arrays have a fix size. The size has to be specified when the array is created.
Second, UnityScript is based on Javascript, not Java.
Third, this line: for(int z=0;z<WorldSize;z++)
would be valid C# code, but not UnityScript. Variable declarations in Javascript always needs the var keyword.
I want to point out that I asked a question that was specifically directed at Java/JavaScript, which makes the first part of your answer irrelevant. However, you weren't wasting your breath. While searching for specific questions on Answers, I often misread the title of a question, only to discover that one or more of the answers is exactly what I was looking for.
It goes to show that you can never give too much information, just so long as it doesn't result in a convoluted and difficult-to-read article.
It wasn't really irrelevant since i can remember that UnityScript doesn't support creating multidimensional arrays before version 3.2
I never use UnityScript so i'm not up-to-date, but Eric our UnityScript guru has written this little helper class for UnityScript which is now obsolete since they implemented mul-dim-array the same way they exists in C#.
Also just as a sidenote: Java is not Javascript. Actually C# is much closer to Java than Javascript or UnityScript is.
@Bunny83 2 years later, I realise you posted another comment. I didn't get a notification because you didn't @ me.
Ah, yes, I would agree with that; it was up-tight of me to say any answer was irrelevant when I really didn't know what I was talking about.
The reason I use UnityScript is because it's the closest of the three to C++. Not in syntax necessarily, but in code flow. Actually, now that I think about it, it's closer to BASIC than C++. Start at the top, create global variables, create functions. Simply, easy, straightforward, and UnityScript is way more flexible than C++.
Anyway, I'm ranting needlessly, and someone FINALLY approved my question! :D Hopefully I can get back to having fun creating an awesome game in Unity!
Your answer
![](https://koobas.hobune.stream/wayback/20220613065815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Script wont read array 2 Answers
JSON missing arrays 1 Answer
WARNING : Assignment to temporary. 1 Answer
Warning on creating an array of class objects 3 Answers
Need my function to work with different lists of different values (classes) 1 Answer