Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Clonkex · Apr 02, 2012 at 05:50 PM · arrayclass

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Apr 02, 2012 at 07:05 PM 0
Share

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.

avatar image Clonkex · Apr 03, 2012 at 07:25 AM 0
Share

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?).

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Clonkex · Apr 03, 2012 at 11:28 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Clonkex · Apr 03, 2012 at 11:36 AM 0
Share

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.

avatar image Bunny83 · Apr 03, 2012 at 11:46 AM 0
Share

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.

avatar image Clonkex · Apr 20, 2014 at 07:37 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges