Hey so I have two scripts, they are very basic. I never tried this before so I thought I would start simple. One is for the actual inventory and the other for items. You should be able to make you what it all means. I’ve hit a point where I have more questions on how to continue than actual idea to do. If someone could please at least guide me on what I would need to do a simple inventory. Right now I’m looking to be able to pick up an item (Disable renderer and child to player) and place in the inventory slots, also icons for them. Simply be able to click an item to select and then click one of the options in a separate window like use, drop or equip.
How could I store my items
How can I select then and then pick an option
How would I put the item in a not already taken space
How could I make the inventory, how can I use an array to make up a GUI out of it.
Yes I’ve searched it up videos but not many of them show what I need or talk about details.
The code:
#pragma strict
//Inventory
static var inventoryArray : int[] = [0,0,0,0,0];
var displayInventory : boolean = false;
var unzipSound : AudioClip;
var zipSound : AudioClip;
private var barLength = 0.0;
function Start()
{
barLength = Screen.width / 7;
}
function Update()
{
if(Input.GetKeyDown(KeyCode.I))
{
displayInventory = !displayInventory;
if(displayInventory)
{
audio.PlayOneShot(unzipSound);
}
else
{
audio.PlayOneShot(zipSound);
}
}
}
function OnGUI()
{
if(displayInventory)
{
GUI.Box(Rect (0,0,100,50), inventoryArray[0]); //the part where I wanted to display the array
}
}
#pragma strict
//item
var inventory : Inventory;
function OnTriggerEnter(col : Collider)
{
if(col.tag == "Player")
{
gameObject.active = false;
inventory.inventoryArray[0]++;
}
}
Just put the item object into the next free inventory array slot. (InvArray[NextFreeSlot] = ItemObject)
“How can I select then and then pick an option”
In OnGUI(), if the inventory should be shown, iterate through all slots of the inventory array and if the slot is not empty, draw the icon by accessing the icon graphic of the item gameobject stored in that slot. Either way, draw a button at the required coordinates where the slot should be shown and add some if statements when it’s left clicked: If the slot isn’t empty, pick up the object (make the slot null), …
and if it’s right clicked, display an options dialogue etc…
“How would I put the item in a not already taken space”
Before picking up an item, call a small function that finds the next free inventory slot and returns this slot’s number. You can then use this number to let the game know at which position of the inventoy array your new item should be stored.
“How could I make the inventory, how can I use an array to make up a GUI out of it.”
Remember to put System.Collections.Generic; at the top of any scripts using List<>
To answer your question. Start with creating a Class for the individual items:
[System.Serializable]
public class Item{
public int identifier;
public string itemName = "";
public string description = "";
...ETC...
}
Next, you need a way to store these items. create a database:
public List<Item> itemDatabase= new List<Item>(); // this is the core of the database, after weve created an item, we add that item to the list (see below when i make an item)
void Awake(){
Item sword = new Item();
sword.identifier = 1; // the identifier has to be unique to each item
sword.itemName = "Sword";
sword.description = "Hit things with it" // this could be used in a tooltip
itemDatabase.Add(sword); // weve added the item to our database
...ETC...
}
Now you need a way to iterate through the itemDatabase youve created…
public Item GetItem(int identifier){ //remember, we set up unique identifiers above
for(int i = 0; i< itemDatabase.Count; i++){
if(itemDatabase*.identifier == identifier){*
return itemDabase*;* } } return null; } Now weve created individual items, an item database, and a way to find those items. How you set up the UI is up to you. I would break it up into two things. The inventory and the inventory slots. Each has their own script. -Inventory manages the slots. -Invnetory slots manage the items/UI. setting up a slot… public Item item; // the item this slot contains
//Add other parameters here for dragging/dropped/etc adding an item to a slot… public List slots; // add the slots to this list
public void InventoryAddItem(Item theitem){ //Forloop through the slots, specify how you want the item dropped. Is the slot full? Is the item stackable? for(int i= 0; i< slots.Count; i++){ //if the slot isnt full slots*.item = theItem;* }
} The inventory would have a list of available slots. If you are using NGUI you can utilize the “OnDrop” “OnPress” events to manage picking up/moving/dropping items from slot to slot. ForLoop is your best friend. Good luck _*http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops*_