Best type of array to use in c# for a large group of items with indexes

In my game, I am at the point of creating an inventory system. There are a large number of items that can be found, purchased, crafted, etc.

Instead of using a random generator for items and their attributes, I want specific items to be created for whatever: a chest, loot, store, etc. What is the best way of creating an array with an index that I can use to determine what set of items to be created in a set of circumstances.

This chest contains numbered items 4,5,7,21
a sword, an axe, a fur boot and leather armor.

The store in this city has items 14-32 only.
an antidote potion, water, endurance potion, healing herbs, grape juice, healing potion and light crossbow.


There’s a lot of info on which array to use here: Unify Community Arrays. It sounds like what you want is the built in array since you won’t be changing the size of it at all, and built in is the fastest if you don’t need dynamic sizing.

You can accomplish what you want two ways, either make one big ItemController script that has a list of all the possible items and what they have or do, your 0-32 different items. Then you can in your chest have a mini array with ints that reference your ItemController array to find out which items are in the list. So this array would be the one that has 4,5,7,21 in it. Here’s how you declare them and reference them:

// Here's the declaration in ItemController, drag all the item GameObjects to it in the inspector
 Public GameObject[] itemList;

 // Here's the chest script stuff, declaration then function for use
 Public int[] itemsInChest; // This is the items you want in the chest, 4,5,7,21 or whatever
 Public GameObject itemController; // This is the ItemController object you made that holds all of the item references.

 void OpenChest()
    // This is where you would open your chest and transfer items to player inventory or something.
    foreach ( int item in itemsInChest)
       // Transfer them to the player
       itemController.itemList[item].GetComponent().GiveInstanceToPlayer() // This is the call to the item script that       gives the item to the player


You could also skip the int list, instead just referencing the item directly in each chest, and then have:

Public GameObject[] itemsInChest;

And call it with

foreach (GameObject item in itemsInChest)

Holding references to an item multiple times takes up more memory, so if you hold ints in the chest that point to where you can find the item it will be faster when you start to have a bunch of chests. It’s a bit more complicated though. Make sense?