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 VIIKnight · Jan 06, 2015 at 12:35 PM · arrayclasshowcreate

How to create a 2d array of a class

Hi, I am having trouble creating a 2 dimensional array of a class. I have found a few posts on this subject on unity answers and tried to mimic the code in them but to no avail. Here are the 4 posts that I have found. first post, second post, third post, forth post. I noticed this line of code in 2 of the posts...

 T`ileInfo[,] map = new TileInfo[width,height];` 

 public BlockIDClass [,] gridBlocksID    = new BlockIDClass [10,10];

and in a third post they omitted the "," in the [].

 private MyEvent[] myEventList = new MyEvent[10] ;

Is there a reson to the ",". What does it do?

here was my test code for a 1d and a 2d array of a class.

this was the 1d array and error.

 using UnityEngine;
 using System.Collections;
 [System.Serializable]
 
 public class Classtest : MonoBehaviour {
 
 
 
     public class Stuff
     {
         public int bullets;
         public int grenades;
         public int rockets;
         
 
     }
     public Stuff [,] myStuff = new Stuff[1];

this was the error after saving script.

Assets/scripts/Classtest.cs(17,48): error CS0029: Cannot implicitly convert type Classtest.Stuff[]' to Classtest.Stuff[,]'

and the code for the 2d array and error.

using UnityEngine; using System.Collections; [System.Serializable]

public class Classtest : MonoBehaviour {

 public class Stuff
 {
     public int bullets;
     public int grenades;
     public int rockets;
     

 }
 public Stuff [,] myStuff = new Stuff[1][1];

this was the error for the 2d array.

Assets/scripts/Classtest.cs(17,50): error CS0178: Invalid rank specifier: expected ,' or ]'

Reading about the subject it seems a lot of people suggest using a generic list. I am creating a game that has a grid of "gamemats" as gameobjects like a chess, or checker board, and I use a 2d array to reference each of them so another 2d array would be ideal for me because of the "posx", "posy" variables I use to reference them. I don't see how using a list would work as well for me in this situation. Any help on correct syntax or what I did wrong would be very appreciated.

thank you.

Comment
Add comment
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

1 Reply

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

Answer by Cherno · Jan 06, 2015 at 02:04 PM

Ok, first of all: The official MSDN documentation is always a good start for researching non-Unity-specific C# coding stuff:

Multidimensional Arrays (C# Programming Guide)

Is there a reson to the ",". What does it do?

the "," just indicates that there is more than one dimension to an array. In the example code you posted, it seems like there is an error in the syntax.

A very handy guide to arrays, lists and similar is this one here, highly recommended:

Which Kind Of Array Or Collection Should I Use?

A one-dimensional array is constructed like this:

 int[] intArray = new int[10];//creates an array of 10 int elements

A two-dimensional array is created like this:

 int[,] intArray = new int[2,4];//creates a two-dimensional array that has a length of 2 in the first dimension and 4 in the second dimension. The dimensions could be thought of as the rows and columns of a spreadsheet. 
 

Note that the size of a one-dimensional array is accessed with

 intArray.Length

, a multi-dimensional array has multiple sizes, one for each dimension, and they are accessed with

 intArray.GetLength(0)

for the Length of the first dimension,

 intArray.GetLength(1)

for the second dimension, and so on.

Finally, for your example, you would construct your multi-dimensional array like this:

 public class Stuff
 {
      public int bullets;
      public int grenades;
      public int rockets;
      
  }
 public Stuff [,] myStuff = new Stuff[1,1];
 
 //this would create a two-dimensional array of your Stuff class with 1 rows and 1 columns... Which wouldn't make much sense because it could only hold as much data as a normal non-array variable.

If you would like to create a larger 2d array, you can declare and access it like this:

 public Stuff [,] myStuff = new Stuff[2,3];
 
 //creates a 2d array with 2 colums and 3 rows.
 //access the contents like this:
 
 int myBullets = myStuff[1,2].bullets;





Comment
Add comment · Show 2 · 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 VIIKnight · Jan 07, 2015 at 02:03 AM 0
Share

Well first let me thank you in your response and answer. It took me copying and pasting your line of code right under $$anonymous$$e and comparing them character for character, space for space to see the difference.

     public Stuff [,] myStuff = new Stuff[1,1]; 
     public Stuff [,] myStuff = new Stuff[1][1];

It's so plain to see it here. I can't believe how long this took me to figure out and even see the difference. I liked C# for coding in unity because it is so close to C++. Which was my first language I learned to code in. I seriously did not see that comma between the 2 element numbers. I am so used to the whole double brackets [1][1].

So thank you! On another note your link to the msdn documentation was in German, I don't speak German, but I did find the English version. Very nice source. I had also had read the array guide a few week earlier. Again very informative. So again thank you.

avatar image Cherno · Jan 07, 2015 at 02:30 AM 0
Share

Glad to be of help. Sorry about the non-english language link, for me it showed as english (which I prefer myself even though I'm a native German speaker) because it automatically switches to the German $$anonymous$$SDN site but with optional English translation. I guess I forgot that little detail :) I edited my answer with the correct link so other users won't have the same problem :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why cant i do this with an array? 1 Answer

Writing an class array to disk? 1 Answer

Inspector variables from class in List are always initialized to its default values 1 Answer

How can I call a function of a class array from another function of a class 1 Answer

Declaring Variables in an Array of a Class 0 Answers


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