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 hakermania · Jul 01, 2013 at 07:07 PM · javascriptarrayclassdynamic

Creating a dynamic array of objects of custom class

I've created a custom class and I'm trying to store in an array some objects of this class. I need the array to be dynamic.

I cannot use 'push' in unityscript as in arrays in javascript because the compiler complains that:

 'push' is not a member of 'InvItem[]'

That's my code:

 var itemCount : int = 3;
 var items : InvItem[] = new InvItem[itemCount];

but I cannot insert/delete items in that array.

How is that possible? Can I use something similar to malloc and free combination, as in C?

What I am trying to do is to have a place so as to store all my inventory items so as to know how to effectively draw a GUI ScrollView (how many items there are, what are the names of each one, how many times each one appears etc).

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 robertbu · Jul 01, 2013 at 07:09 PM 0
Share

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F

Generally I see the generic list class used, but you can use any of the collection types:

http://msdn.microsoft.com/en-us/library/system.collections.generic.aspx

avatar image equalsequals · Jul 01, 2013 at 07:16 PM 0
Share

.Net Arrays (depicted as Type[]) do not have the ability to be resized, a feature which makes them very fast.

You should try using the generic List which is resizable through its methods like Add and Remove.

1 Reply

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

Answer by iwaldrop · Jul 01, 2013 at 09:30 PM

I believe this is what you're looking for. Unity's "javascript" implementation is (IMO) garbage. If you're going to stick with Unity you'd be better off switching to C# sooner than later.

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 hakermania · Jul 01, 2013 at 10:05 PM 0
Share

The opinions differ on this one. I used the 'List' implementation and it worked just fine. Can you please provide me with some arguments on why C# > unityscript ? A link maybe?

avatar image iwaldrop · Jul 01, 2013 at 10:28 PM 0
Share

Here, here, here, and here. :)

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

17 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

Related Questions

Create an array of classes, of different type. 1 Answer

Reading and Storing External Data into Memory (From Text) 1 Answer

Referencing subclass in Unity JS 2 Answers

Setting up an array of Items 1 Answer

Different way to access class variables? 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