- Home /
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).
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
.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.
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.
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?