Store multiple types of data in an Array?

Evening everyone! Here’s the deal:
I’m setting up an inventory system the stores multiple kinds of information on the items that occupy each slot. Each inventory slot needs to house the object’s 1) Name, 2) Tag, 3) Prefab Object, 4) Amount, 5) MaxStack.

The way I’ve set this up for versatility, is that every object that can be ‘collected’ is a Prefab that includes a script that houses the above information in separate variables, and when the player interacts with the object, I need it to package that info into an Array or List, SendMessage(“Inventory”, Array/List) to the player’s inventory script, and be unpacked by a function in that script to compare the item’s info against what’s already stored to find the next available space.

SO! In theory, this should work using Javascript Array or an ArrayList, but on unpacking, I repeatedly get Implicit Downcast warnings from type ‘Object’ to type ‘String, int, etc.’, even with type casting like:
var itemName : String = array[0];

I’m working in UnityScript(variant of Javascript), but translating to/from C# isn’t hard, so any help would be appreciated. :slight_smile:

Let’s say your class looks like this :

public class MyClass {
    public int myInt;
    public string myString;
    public bool myBool;

In your other scripts, you can have :

List<MyClass> list = new List<MyClass>();

When “unpacking”, it should look like this :

MyClass unpackedItem = list[0];

Hey, check this out, Hope it helps!

var data = new object { 1, “string”, 1.2f, 2 };

This data format is useful for for sending JSON messages, for example using UltimateJson:


Which will result in such array (this approarch generates an object instead of array when serialising classes):


Also you can store this data in a database like MongoDB

you can use simply the type ‘object’ as an array and pass into it the variables… when you need one of them you can check is original type with GetType()