Hotbar and item pickups?

So ive been looking around sometime now for a tutorial that shows this and nothing has come up that i have found useful.

What i am wanting to do is have a hotbar at the bottom of the screen which has 10 boxes that are all numbered in ascending order. If the player walks over something that he is able to pickup, then it will be sent onto the hotbar where you can equipt or use it by pressing the number of the box the item is in.

Please can someone help me out? I hope this picture helps explaining things.

14751-hotbar.png

There is a free inventory system in the asset store which might help to get you jump started:

As you will appreciate, an inventory is essentially a GUI with some sort of backing data structure. Here is a very simple example of how you might go about achieving this with the standard Unity GUI. You will need some sort of data representation to describe an inventory item. There are a number of ways to achieve this, this is just one way!

Note: The following has not been tested, but should demonstrate the basic idea :slight_smile:

Find similar UnityScript translation of following example here: // InventoryItem.js// Icon texture for GUI.var icon:Texture2D;// Label f - Pastebin.com

Inventory Item Behaviour

Attach script to each inventory item, and perhaps even use as a base class when additional functionality is needed.

using UnityEngine;
public class InventoryItem : MonoBehaviour {
    // Icon texture for GUI.
    public Texture2D icon;
    // Label for GUI.
    public string iconLabel;

    // other item data which you may need...

    public virtual void Activate() {
        // Activation logic for inventory item.
        gameObject.SetActive(true);
    }

    public virtual void Deactivate() {
        // Deactivation logic for inventory item.
        gameObject.SetActive(false);
    }
}

Inventory Behaviour

Create game object to manage your inventory and then associate inventory items using inspector.

using UnityEngine;
public class Inventory : MonoBehaviour {
    // Fixed length array of items, though you may want to use
    // the generic `List` class if you wanted varying length.
    public InventoryItem[] items = new InventoryItem[10];

    // Represents the active inventory item.
    public InventoryItem activeItem;

    // Temporary content instance.
    private GUIContent _itemButtonContent = new GUIContent();

    void OnGUI() {
        Color restoreColor = GUI.contentColor;

        GUILayout.BeginArea(new Rect(0, 0, Screen.width, 70));
        GUILayout.BeginHorizontal();

        // Draw buttons for inventory items.
        foreach (InventoryItem item in items) {
            if (item == null) {
                // Just show a blank non-clickable button.
                GUI.enabled = false;
                GUILayout.Button(GUIContent.none);
                GUI.enabled = true;
            }
            else {
                // Show clickable button.
                _itemButtonContent.image = item.icon;
                _itemButtonContent.text = item.iconLabel;

                // Highlight active item.
                GUI.contentColor = (item == activeItem)
                    ? Color.red
                    : restoreColor;

                if (GUILayout.Button(_itemButtonContent))
                    ActivateItem(item);
            }
        }

        GUILayout.EndHorizontal();
        GUILayout.EndArea();

        GUI.contentColor = restoreColor;
    }

    public void ActivateItem(InventoryItem item) {
        // Don't bother, this item is already active!
        if (item == activeItem)
            return;

        DeactivateItem();

        // Active inventory item!
        activeItem = item;
        item.Activate();
    }

    public void DeactivateItem() {
        // No item is active, bail!
        if (activeItem == null)
            return;

        activeItem.Deactivate();
        activeItem = null;
    }
}