Inventory Database.

Hey guys, so i am doing an inventory database, however i can’t seem to be abble to make it. I am adding the items in a list, however they won’t show in the inspector like their name etc.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryDatabase : MonoBehaviour {
    public List<Item> items = new List<Item>();

    void Start () {
        registerItems ();
    }
    private void registerItems(){
        //Apple
        Item preset = new Item();
        preset.itemname = "apple";
        items.Add (preset);
    }
 
    void Update () {
    
    }
}


As you can see it appears as “None(Item)”. I was expecting it to show as Apple: and then itemname, id etc…

Hey, This can be achieved by using a serialization class for your item(s) for the Item script as shown below.

This can include all of the variables and functions you need for an item.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Item : MonoBehaviour {

    [System.Serializable]
    public class ItemProperties
    {
        public string ItemName;
        public int ItemID;
    }

}

And then storing all of these in a list of ItemProperties in your inventorydatabase

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryDatabase : MonoBehaviour
{

    public List<Item.ItemProperties> ItemProperties; // List of item properties to show in inspector

    void Start()
    {
        ItemProperties = new List<Item.ItemProperties>(); // initialize list
        registerItems();
    }
    private void registerItems()
    {
        //Apple
        Item.ItemProperties preset = new Item.ItemProperties();
        preset.ItemName = "Apple";
        preset.ItemID = 1; 
        ItemProperties.Add(preset);
    }

}

Results shown here:

3214693--246154--Databaswe.png

Thank you so much! It works just as i wanted it to.
Also, should i use a text file or a .xml file to store the item database and load it from there?

I Havent ever tried this. The most ive done with file saving is using a json file to save some variables for a menu system. But i would recommend using xml as theirs many tutorials on it Saving and Loading Data: XmlSerializer - Learn Content & Certification - Unity Discussions

Thanks. Also i am sorry for extending this, but i am kinda a starter on csharp and unity, promiss this is the last question. How can i get a specific value inside that item database list? like if i wanted to get the id of the Apple item, because i will need it when adding the item to the inventory later on.

You can do this by accessing the index of the list, i added a function in this example

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryDatabase : MonoBehaviour
{

    public List<Item.ItemProperties> ItemProperties;

    void Start()
    {
        ItemProperties = new List<Item.ItemProperties>();
        registerItems();
        DisplayItems();
    }
    private void registerItems()
    {
        //Apple
        Item.ItemProperties preset = new Item.ItemProperties();
        preset.ItemName = "Apple";
        preset.ItemID = 1;
       
 
     
        ItemProperties.Add(preset);
    }

    public void DisplayItems()
    {
        Debug.Log(ItemProperties[0].ItemID); // // itemProperties[0] is the first element of the list, and displays the ID

        if (ItemProperties.Count > 0)
        {
            for (int i = 0; i < ItemProperties.Count; i++)
            {
                Debug.Log(ItemProperties[i].ItemID); // same purpose but iterates through a list
            }
        }
    }

    void Update()
    {

    }
}

You can also use if statements to check for certain items such as

        if (ItemProperties.Count > 0)
        {
            for (int i = 0; i < ItemProperties.Count; i++)
            {
                if (ItemProperties[i].ItemName == "Apple")
                {
                    // do something
                }

                if (ItemProperties[i].ItemID == 1)
                {
                    // do something
                }
                Debug.Log(ItemProperties[i].ItemID);
            }
        }