Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by alienheretic · Feb 15, 2010 at 09:43 PM · arraymultidimarraybce0077

How to Make 2 dimension array of classes

i have the following class

using UnityEngine; using System.Collections;

public class Tile_Info { int ID; int Varriation; }

i would like to access it using simillar to this

maparray[0,0].tile_id=0

can someone place the code here to help me figure out how to do this please

Comment
Add comment · Show 1
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 Ashkan_gc · Feb 16, 2010 at 05:48 AM 0
Share

duck is right. i just want to say. use a private array inside your tile class for your tiles and change your design method.

1 Reply

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

Answer by duck · Feb 15, 2010 at 09:57 PM

First I'm changing the names slightly to adhere to Unity (and c#) coding conventions. These aren't errors but it's good to stick to:

"Tile_Info" becomes "TileInfo"
"maparray" becomes "map"
"Varriation" becomes "variation"

Now, declare the array like this:

TileInfo[,] map = new TileInfo[width,height];

Next, make sure you populate the array cells with instances of TileInfo, using something like this for each cell which contains a tile:

// fill out the empty array with TileInfo instances:
for (int x=0; x<width; ++x) {
    for (int y=0; y<height; ++y) {
        map[x, y] = new TileInfo();
    }
}

Then you can use the array like this:

TileInfo thisTile = map[x,y];
thisTile.ID = someValue;
thisTile.variation = someValue;

Or directly access the tile's variables:

map[x,y].ID = someValue;
map[x,y].variation = someValue;

And the same for testing:

if (map[x,y].ID == someValue) { ... }

hope this helps!

Comment
Add comment · Show 6 · 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 Statement · Feb 16, 2010 at 04:55 PM 1
Share

Don't forget that you have to initialize each element in map before you use them, as such map[x, y] = new TileInfo(); Built-in arrays use default(T) to initialize the array elements, and for reference types that is null.

avatar image duck ♦♦ · Feb 16, 2010 at 05:40 PM 0
Share

good call Statement! (adding this to answer)

avatar image alienheretic · Feb 17, 2010 at 12:02 AM 0
Share

well i tried this but i am a bit confused

using UnityEngine; using System.Collections;

public class TileInfo { public int Tile_ID; public int Tile_$$anonymous$$od; }

public class Tile_$$anonymous$$ap {

public static TileInfo[,] map = new TileInfo[32,32];

}

and trying to access it from jscript like this

var TileArray=Tile_$$anonymous$$ap.map(34,34);

obviously i have no idea what i am doing or i wouldnt get errors

can someone please post the working code for this?

avatar image duck ♦♦ · Feb 17, 2010 at 10:02 AM 0
Share

Like Statement pointed out (and I added it to the example), simply creating the array does not create the tile instances that you want inside the array. I will update the answer with a more detailed example.

avatar image alienheretic · Feb 17, 2010 at 03:14 PM 0
Share

i treid this and tried accessing from java like this Tile_$$anonymous$$ap.map(0,0).Tile_ID=7; and i get this error again i must be doing this wrong Assets/Scripts/tilemap.js(104,18): BCE0077: It is not possible to invoke an expression of type '(TileInfo, 2)'. using UnityEngine; using System.Collections; public class TileInfo { public int Tile_ID; public int Tile_$$anonymous$$od; } public class Tile_$$anonymous$$ap { public static TileInfo[,] map = new TileInfo[34,34]; void main() { for (int x=0; x<34; ++x) {
for (int y=0; y<34; ++y) {
map[x, y] = new TileInfo();
}}}}

Show more comments

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

1 Person is following this question.

avatar image

Related Questions

trouble with 2 dimensional arrrays in javascript 1 Answer

How to declare a multidimensional array of strings in c#? 2 Answers

Movement through an array over time 2 Answers

rotating vertices with for loop in 0 Answers

Sending value stored in an array to another script with cSharp 3 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