- Home /
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.
Answer by numberkruncher · Aug 25, 2013 at 05:03 PM
There is a free inventory system in the asset store which might help to get you jump started: https://www.assetstore.unity3d.com/#/content/10384
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 :)
Find similar UnityScript translation of following example here: http://pastebin.com/VSQYMnKS
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;
}
}
In the second script on line 11 I get, "A new expression requires () or [] after type" Not to sure what this means?
Sorry that was a typo (fixed), like I said the above has not been tested so there may be a couple of $$anonymous$$or syntax errors ;)
Thanks for that the Gui side of things is perfect :) but now I need the items to become active when their button is active. Could you help with this?
Im thinking if all the items are held children of the main camera but marked as inactive, when their button is clicked it is marked as active and can then be used?
Each InventoryItem
has an activate and deactivate method which is automatically called upon selecting the GUI button. I have updated the example so that it activates/deactivates the item game object also. Was this what you meant?
Yeah that's great thanks! As for picking items up is there a way to only be able to select an item if you have picked it up from the ground?
When the player comes into contact with an item which can be picked up, it adds it onto the hotbar where you can then select it.
Your answer
Follow this Question
Related Questions
Scripting help (sending messages?) 1 Answer
Script Not Working 0 Answers
Adding Item object to Inventory List 0 Answers
Is there any easy way to keep buttons inside of box? (c#) 1 Answer
Simple Backpack "Pick up and drop" 1 Answer