- Home /
how do i display this as GUI
Sorry for the inpasific "this" but anyway I have an inventory script that I believe works but Im not sure how to display as GUI for the actual inventory part that people can see in game. Heres the code I have- using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Inventory : MonoBehaviour { // This struct holds the information about an object in the inventory:
public struct InventoryItem
{
// This is a reference to the item info provided by your class
// It should contain information on the item type, and also a picture for use in the inventory.
public Item Item_Info;
// Quantity of item held:
public int Number_Held;
// Location of the item in the inventory grid:
public int X_Location;
public int Y_Location;
public InventoryItem(Item item,int number, int x_Loc, int y_Loc)
{
Item_Info = item;
Number_Held = number;
X_Location = x_Loc;
Y_Location = y_Loc;
}
// Increase the number of objects held (or decrease, by passing a negative number):
public void IncreaseNumber(int number)
{ Number_Held += number; }
}
public Dictionary<string, InventoryItem> m_Inventory = new Dictionary<string, InventoryItem>();
int next_Free_X = 0;
int next_Free_Y = 0;
int width = 10;
void OnTriggerEnter (Collider pCollider)
{
// Check that the object can be picked up here
if(true)
{
// If we already have this object, increase the number we have
if(m_Inventory.ContainsKey(pCollider.name))
{
m_Inventory[pCollider.name].IncreaseNumber(1);
}
else
{
// If we don't have this object, add a new entry to the inventory
m_Inventory.Add(pCollider.name, new InventoryItem(GetItemInfo(pCollider), 1, next_Free_X, next_Free_Y));
// Find the next free inventory slot for new items:
next_Free_X++;
if(next_Free_X >= width)
{
next_Free_X = 0;
next_Free_Y++;
}
}
// Destroy the object picked up:
Destroy(pCollider.gameObject);
}
}
private Item GetItemInfo(Collider pCollider)
{
// You will need to implement this, you could either have a central list (contained in a gameObject)
// holding references to all possible items, or you could retrieve the information from the object you hit
// with:
return pCollider.GetComponent();
}
}
how do I display this into GUI.
Could you edit your post to fix the posted script, please? It got totally messed up. A paint to read.
Even better; If you're not familiar with how to use an OnGUI function, then don't paste in a 150 line script from someone else. Doing so will drown you. Start by tutorials to understand what you're doing. The harsh truth is no one ever success without understanding what they are cutting together.
please edit provided code to increase readability either precede each line with 4 spaces, or in the toolbar that appears above the box click the box with "101010" between " and attachment.
(note this paragraph was edited down from 2000 chars): aside from placing an if(true)
immediately inside any method especially inside of a Unity event without a break anywhere inside is lacking an understanding of what an if
directive is even used for, or what an the directive even does if(true == true)
will always result in true
.
technically your question has no definite answer. if you could show us an "honest" attempt that did not work, or something to that extent we can give you an answer, but otherwise you are asking either for opinions, or "do this for me".
it would appear that the creator of this (guessing tutorial) expects each person to implement their own Item, and OnGUI. considering that this implementation looks like it belongs as a list.
Answer by vcjr12 · Jun 28, 2013 at 04:40 PM
To be able to display Items on the GUI, you would have to put a OnGUI() method somewhere on your script.
void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button"))
print("You clicked the button!");
}
More Info on how to use the GUI can be found here and here ,