Inventory System

Checked with version: 2017.3


Difficulty: Beginner

The inventory system is made up of two parts, the Inventory Controller Component and the Inventory Item.

The Inventory Controller should be added to a GameObject holding the inventory. An Inventory Item script should be added to the item that is being collected. For example Ellen (Inventory Controller) collects a Key (Inventory Item)

The Inventory Controller is able to automatically collects the Inventory Item on contact with their colliders, and defines actions that happen when a GameObject is received or removed.

Inventory Controller

This Component has an entry for each item that can be carried in your inventory. The Component can exist on a character, a chest or a shop.

  • Inventory Events: List of events to occur on specific GameObjects added to the inventory. Increase the number here to add new events (tip: you can delete an event by right clicking on the name in the list and choosing Delete Array Element).
    • Key: A unique identifier that represents the Inventory Item e.g. Key, Gun, Staff. This must match the Key in the Inventory Item component on the item you wish to be collected. The inventory can only hold a single instance of each GameObject; if a GameObject with the same Key is already in the inventory, the add event in Inventory Controller is not called.

    • OnAdd: Events triggered when the item is added to the inventory.

    • OnRemove: Events triggered when the item is removed from the inventory.

    • Data Settings: See the Persistent Data documentation for more info.

Do not confuse the Key of an item (which is the unique identifier assigned to an item) with a key, a collectible GameObject we like to use in games. Mention within the documentation of the unique identifier is in bold to limit confusion.

Inventory Item

Any item that can be carried in the inventory needs an Inventory Item Component. This specifies the Key of the item, an Audio Clip and which Layers can pick the item up.

An InventoryItem is a component added to the GameObject representing the item. It also requires a Circle Collider 2D and automatically adds one if missing. That collider is set to trigger and is used to detect when the Player touches the GameObject.

The state of the GameObject (active or not) is saved, so it remains the same when the Scene is reloaded and the Player re-enters the zone.

  • Inventory Key: A unique name for the item (e.g. Key1), used by the inventory to track if an item is already owned or not.

  • Layers: The Layer the GameObject collecting the item is on.

  • Disable On Enter: This setting disables the GameObject once it has been collected.

  • Clip: The Audio clip to be played when the item is collected. If empty, no clip is played.

  • Data Settings: See the Persistent Data documentation for more info.

Related tutorials