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:
[System.Serializable]
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.