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 jimmycrazyskills · Feb 12, 2014 at 01:56 PM · javascriptarraylistclassesof

How to use Javascript Array with classes?

Hi , I have checked the unity website for how to use the arrays but still am confused and get errors!

I'm trying to make a simple inventory and currently have a class called item.

 class Item{
     var damage : int;
     var name : String;
     var model : GameObject;
     var icon : Texture;
 }

However I don't know how to put this into a array , I am currently using the code below to just get the console to out the name of an item. I also seem to be getting different results depending on whether I select the "maximize on play" button in the editor.

 var Inventory : Item[];
 
 
 function Start () {
 Inventory = new Item[10];
 Inventory[1].damage = 1;
 Inventory[1].name = "hello";
 }
 
 function Update () {
 print(Inventory[1].name);
 }

Any help on this topic , I will be extremely greatful for , Thanks =)

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
0
Best Answer

Answer by robertbu · Feb 12, 2014 at 02:10 PM

You not only have to create the array (i.e. new Item[10]), but you have to create each individual item that goes into the array. For example:

 #pragma strict
 
 class Item{
     var damage : int;
     var name : String;
     var model : GameObject;
     var icon : Texture;
 }
 
 var Inventory : Item[] = new Item[10];
  
 function Start () {
     Inventory[1] = new Item(); // <<========
     Inventory[1].damage = 1;
     Inventory[1].name = "hello";
 
     Debug.Log(Inventory[1].name);
 }

Note I've only create one item for entry index 1. Instead you might want to populate the entire list:

 for (var i = 0; i < Inventory.Length; i++) {
     Inventory[i] = new Item()
 }
  
Comment
Add comment · Show 5 · 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 jimmycrazyskills · Feb 12, 2014 at 02:23 PM 0
Share

Hi , thanks for answering so quickly! I still have an issue though with getting values from the array , with the code below I keep getting this error "Array index is out of range".

 function OnGUI(){
         if(Input.Get$$anonymous$$ey("i")){
              if(Inventory[0].name == "hello"){
                        //////GUI CODE IS HERE       
              }
  
         }
 }
avatar image robertbu · Feb 12, 2014 at 03:18 PM 0
Share

As an educated guess. Either 1) make Inventory private:

 private var Inventory : Item[] = new Item[10];

Or 2) move the 'Inventory = new Item[10]' back into Start().

Since your Inventory is public, it is taking its size from the Inspector rather than the initialization here. The initialization of public variables is only done at the time the script is attached, and it is likely that you already had the script attached when you made the changes.

avatar image jimmycrazyskills · Feb 12, 2014 at 04:20 PM 0
Share

You Sir , are awesome! :)))

avatar image Hoeloe · Feb 12, 2014 at 05:41 PM 0
Share

I should point out that robertbu's original code, setting only Inventory[1], is rather misleading. Inventory[1] is actually the second element of the array (with Inventory[0] being the first). Arrays are zero indexed. I realise that, judging from your code above, you already know this, but I feel it's worth clearing up for others who might not.

avatar image jimmycrazyskills · Feb 12, 2014 at 10:11 PM 0
Share

yeh cheers did know already but thanks for the heads up :)

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

19 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

Related Questions

Make an array containting multiple classes 1 Answer

Accessing variable from a class 1 Answer

Access a variable of a class stored in an array. 2 Answers

How to find the opposite variables list? 1 Answer

Inspector variable MonoScript, using for creating a List? 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