Inventory system?

I need help creating an inventory system much like Minecraft’s (minus the crafting). I have somewhat of an idea of what I have to do, but all the tutorials I look at or articles I read that explain how to make an inventory system don’t seem to fully explain everything. It has really been bugging me…

static public Dictionary<> inventory; // For items in inventory

public class Item
{
//For Item creation
}

I need some explanation, and I need to know how to make it work! Answers Appreciated.

You have a start, most of what you need done is done with that snippet actually, from here on all you have to do is define item properties (ex. Name, Type ect…) and create some UI (user interface )handeling

If you’re not already familiar with it, Unity has a great API where you can learn to use all of the different functions provided w/ the unity engine. Here is a link the the API on the subject of GUI: Unity - Scripting API: GUI

Before writing an actual code, i would like talk about couple things, like what is overall basic idea from game creation perspective itself.

Map designers, quest creators and many others, these people mostly never code when creating maps, quests, place npc etc. They have ready custom built tools for doing that, all what they do is move sliders, search through lists and place/write (not code).

So, before actually creating the game experience itself, we need to make sure that we the right tools for doing that process, it will be taking a long time to create the map, so each helpful thing for the map designers can save tens of hours.

One way would be storing ready prefabs in one folder, but imagine if prefab amount is around 4 thousand, considering all little props, items, mobs etc. Artist will drive nuts working like that. So, custom editors is the only option, you could sort specific item lists under different categories, also show how they look or will look. That would be much more easier to work with and focus on map building than trying to find specific item for 10 minutes.

It means, each item, Quest, NPC, monster, gun, attachment, etc should be somehow recognizable (specific number or something), but how? If in simple case all what it has is a tag ?

Creating a structured list for each type of item would be good logic, for example one list for pistols, one for magazines ans so on. When the list is created you can easily access it from any point in your game and custom editors. Which means if you update you food list, your custom editor will automatically will be updated.

So basically when having these lists, you can spawn anything and anywhere And track things via Log.

So first, we create the list (array) which will hold information about the items. This script should be on player itself.

public class Items : MonoBehaviour{

    //We create a Node, possible information each this current item  will hold
    [System.Serializable] //Without this next class "Item" wouldn't not show in inspector
    public class Item{
        public string name; // its name
        public int weight; //just showing the idea
    }

    public Item[] allItems;

    //This is just for showing how it might work
    public InfoAboutItem(int index){ //This will be called from player
        Debug.Log("You picked up " + allItems[index].name + " and it weights " + allItems[index].weight);
   }
}

After attaching script you basically now can create item list in any size you want, and each can contain lots of information.

Now we need a script which will be located on item itself, so we would know what item is that.

public class ThisItem : MonoBehaviour{
    public int itemIndex; // remember that first element is 0 not 1
}

Yes, that’s it :-).

Now the script which will find the item.
Also on player.

public class Player : MonoBehaviour {

    void OnTriggerEnter ( Collider col ){
        if (col.tag == "Item") { //All previous items in game can be under one tag "Item"
            GetComponent<Items>().InfoAboutItem(  col.GetComponent<ThisItem>().itemIndex  );

            Destroy(col.gameObject);
        }
    }
}
  • Set the list in size of 1 (in inspector), in first element fields, enter its name and weight.
  • Place ThisItem script on the pickable item, set index to 0, tag should be “Item”

After triggering the item the text will appear and it will be destroyed.

This is the basic idea how you can create very advanced item in game structures.

A bit of a lecture, but i hope it was interesting to ones just starting :).

i recommend true/false statements such as if something is in slot 1 the slot is false so it check slot 2 i like to use a count to keep track of things that like lets the script check itself