Regarding simple "inventory"/cycle through items without GUI

Hi,

Still a noob so please bear with…

I have been looking for a simple inventory system, without menus/GUI, that would allow me to see an item, pick it up, and be able to cycle through a variety of items that can be held physically, similar to item switching in Minecraft or switching guns in an FPS.

So here’s a scenario:

  • Player holding a stick
  • Player sees torch on ground, runs over and collects torch, torch appears on person and useable
  • Player needs to use stick instead and can cycle back and forth between the two with a button press rather than a menu system

I’m sorry if I’m beating a dead horse, but most of the inventory systems I have found today have been overly complicated for what I need, which is simplicity.

Thank you.

You can implement such an inventory using simple generic List storing your items. List elements could be GameObjects or for example Item scripts attached to your game objects. Here’s a sample code of the script, that could be attached to the player:

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

public class PlayerScript2 : MonoBehaviour {

    public Item currentItem; // you can assign initial item to this and in this case inventory will be initialized with one item

    private int _currentItemIndex = -1;
    private List<Item> _inventory;

	void Start ()
    {
        _inventory = new List<Item>();
        if (currentItem != null)
        {
            _inventory.Add(currentItem);
            _currentItemIndex = 0;
        }
	}
	
	void Update ()
    {
        if (_inventory.Count > 0)
        {
            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                // previous item
                var index = _currentItemIndex > 0 ? _currentItemIndex - 1 : _inventory.Count - 1;
                SelectItem(index);
            }
            else if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                // next item
                var index = _currentItemIndex == _inventory.Count - 1 ? 0 : _currentItemIndex + 1;
                SelectItem(index);
            }
        }
	}

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Item")
        {
            var item = other.GetComponent<Item>();
            _inventory.Add(item);
            SelectItem(_inventory.Count - 1);
        }
    }

    private void SelectItem(int index)
    {
        if (index == _currentItemIndex)
        {
            return;
        }

        currentItem = _inventory[index];
        _currentItemIndex = index;
    }
}

I believe all the names are self explanatory. Item is just an empty class derived from MonoBehaviour.

You can cycle through inventory with 1 and 2 keys. And you can gather items by touching them. I tested this script with first person controller, a plane (ground) and a few cubes and spheres scattered around (with Item script attached, marked Is Trigger in Collider component, and with Item tag).

Of course this script is not complete (item doesn’t disappear after touching, is not displayed on player, etc.) But I leave it to you :slight_smile: