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 Royall · Apr 17, 2014 at 04:23 PM · arraydeclaredimensional

2-dimensional array in JS

Could anyone help me with 2d arrays?

Because I would like to store my item info loaded from an online database in a 2d array like:

 //Item with ID #0
 item[0,0] = "Chain Helmet"; //Name
 item[0,1] = 10; //Sell price
 item[0,2] = "ChainHelmet1"; //Prefab
  
 //Item with ID #1
 item[1,0] = "Plate Chest"; //Name
 item[1,1] = 20; //Sell price
 item[1,2] = "PlateChest1"; //Prefab

Would be nice if the array can hold floats/ints as well as strings (like above example)... I come from GameMaker Studio engine, so don't know if this is possible in Unity. The script reference about arrays is also hard to understand. Also don't know how to declare such an array... Hope you can help me with this one :D

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 robertbu · Apr 17, 2014 at 04:29 PM 0
Share

Specifying C# or Javascript will help in getting an accurate answer.

2 Replies

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

Answer by Royall · Apr 17, 2014 at 05:33 PM

I think i fixed it myself. It looks like this is working:

 class Item {
     var name : String;
     var prefab : String;
 }
  
 static var items : Item[];
  
 function loadItems() {
     items = new Item[2];
     
     items[0] = new Item();
     items[0].name = "Chain Helmet";
     items[0].prefab = "ChainHelmet1";
     
     items[1] = new Item();
     items[1].name = "Plate Chest";
     items[1].prefab = "PlateChest1";
 
     Debug.Log(items[1].prefab);
 }

Is this code correct? Or can it be shorter or something? Thx

Edit: Worked for me :D

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 robertbu · Apr 17, 2014 at 07:32 PM 0
Share

There is nothing wrong with doing it this way, but you can put a constructor in your Item class and then declare the array statically:

 #pragma strict
 
 class Item {
     function Item(n : String, p : String) {
         name = n;
         prefab = p;
     }
     var name : String;
     var prefab : String;
 }
  
 private static var items : Item[]
     = [Item("Chain Helmet",     "ChainHelmet1"),
        Item("Plate Chest",      "PlateChest1"),
        Item("Short $$anonymous$$nife",      "Short$$anonymous$$nife1")
       ];
        
 function Start() {
     for (var item : Item in items) {
         Debug.Log(item.name+", "+item.prefab);
     }
 }
avatar image
0

Answer by Eric5h5 · Apr 17, 2014 at 04:36 PM

You do not want a 2D array, you want to use a custom class, and then have an array of that class.

 class Item {
     var name : String;
     var price : int;
     var prefab : String;
 }

 var items : Item[];
Comment
Add comment · Show 4 · 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 Royall · Apr 17, 2014 at 04:38 PM 0
Share

And how would you store and retrieve info with a class? :)

avatar image Eric5h5 · Apr 17, 2014 at 04:41 PM 0
Share

The same ways you would anything else. Look into serialization, or maybe X$$anonymous$$L, etc.

avatar image Royall · Apr 17, 2014 at 04:45 PM 0
Share

But I need items to be saved with an ID.. So how would I retrieve for example the price of item #5 (ID)

avatar image Royall · Apr 17, 2014 at 05:20 PM 0
Share

Ok I tried the following:

 class Item {
     var name : String;
     var prefab : String;
 }
 
 static var items : Item[];
 
 function loadItems() {
     items[0].name = "Chain Helmet";
     items[0].prefab = "ChainHelmet1";
     
     items[1].name = "Plate Chest";
     items[1].prefab = "PlateChest1";
     
     Debug.Log(items[1].name);
 }

But this doesn't seem to work... Trows me: NullReferenceException: Object reference not set to an instance of an object

I am probably doing it totally wrong... Any help? ^^

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

21 People are following this question.

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

Related Questions

2D array problem in C# 2 Answers

How to access 2d array in outside script 1 Answer

What am I doing wrong with hashtable.CopyTo? 1 Answer

When using FindObjectswithTag(), what order the elements are selected? 1 Answer

Javascript for loops and Arrays. 2 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