Filling in an inventory list.

I want to make a inventory that is in a style of a list that displays various information to the player. However, I’m stuck trying to fill in the field of the button that represents the inventory item. So far I have this:

public class InventoryList : MonoBehaviour {

	public GameObject inventoryButton;

	public List<Item> inventory = new List<Item>();
	ItemDatabase database;

	public Transform contentPanel;

	void Start () {
		PopulateInventory ();

	void PopulateInventory(){

		database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<ItemDatabase> ();

		foreach (var item in inventory) {
			GameObject newInventory = Instantiate (inventoryButton) as GameObject;
			InventoryItem iButton = newInventory.GetComponent <InventoryItem> ();
			iButton.WeaponName.text = item.itemName;
			iButton.WeaponType.text = item.itemType.ToString ();
			iButton.WeaponLevel.text = item.itemLevel.ToString ();
			iButton.UpgradeCost.text = item.itemUpgradeCost.ToString ();
			iButton.ScrapCost.text = item.itemScrapCost.ToString ();
			iButton.maxIcon.SetActive (item.itemIsMaxLevel);
			iButton.Icon.sprite = item.itemIcon;
			newInventory.transform.SetParent (contentPanel, false);

I also have an inventory database where I want to store the various items then create an inventory item when the player gets an item. The database code is:

public class Item {

	public GameObject weapon;
	public string itemName;
	public int itemID;
	public int itemUpgradeCost;
	public int itemScrapCost;
	public int itemLevel;
	public Sprite itemIcon;
	public bool itemIsMaxLevel;
	public ItemType itemType;
	public enum ItemType {
		Primary, Secondary, Consumable

public class ItemDatabase : MonoBehaviour {

	void Start(){


but I’m stuck on how to create the item in the database and have it feed the PopulateInventory. It works fine if I manually add it in the inspector.

So you need a List that acts as your database, right? From this list, various pre-set items can be pulled and added to the player’s inventory. I don’t know why the item database needs it’s own class if it’s just a list anyway, but if you insist then just apply the following code accordingly.

public List<Item> itemDatabase;

void Start() {
     itemDatabase = new List<Item>() {
          new Item() {
               itemName = "Sword",
               itemID = 0,
               itemUpgradeCost = 23
               //... and so on
          new Item() {
               itemName = "Shield",
               itemID = 1,
               itemUpgradeCost = 12
               //... and so on
          //... and so on

Even better would be a Dictionary so we can use the item ID or item name as the key and get the item as a value.

public Dictionay<string,Item> itemDataBaseDictionary;

void Start() {
     foreach(Item item in itemDatabase) {
          itemDatabaseDictionary.Add(item.itemName, item);

You could of course directly create the dictionary without creating a list beforehand.
I suggest looking into XML, it’s a very handy way of keeping things like item databases in a structured way and you don’t have to create items in a script.

Also be ware that if you add an item to the inventory, you merely create a reference in that list, which references the item in the databaase list. That means that if you change the inventory item, it will also be changed in the database list, and you probably don’t want that. This means that you need a function that actually creates a new item in your inventory by copying the values from the database item.

public Item CopyItemValues(Item sourceItem, Item targetItem) {
		FieldInfo[] sourceFields = sourceItem.GetType().GetFields(BindingFlags.Public | 
		                                                          BindingFlags.NonPublic | 
		int i = 0;
		for(i = 0; i < sourceFields.Length; i++) {
			var value = sourceFields*.GetValue(sourceItem);*

_ sourceFields*.SetValue(targetItem, value);_

* return targetItem;*
* }*
You could even use a function that takes any two Types and checks if fields that are present in the source Type are present in the target Type, and copy the values if yes. This has the advantage of not needing to pass the function specific Types (like Item). Handy if you also need to copy values from a vehicle database or whatever. This however, goes beyond the scale of this answer but there are lots of examples on the net, especially on Stack Overflow.