Hey, im trying to add an pickUP script to add the scene item to the inventory but i dont know how.
Here is my Inventory System
public class Inventory : MonoBehaviour
{
public int slotsX, slotsY;
public GUISkin skin;
public List<Item> inventory = new List<Item> ();
public List<Item> slots = new List<Item> ();
private bool showInventory;
private ItemDatabase database;
private bool showTooltip;
private string tooltip;
private bool draggingItem;
private Item draggedItem;
private int prevIndex;
void Start()
{
for (int i = 0; i < (slotsX * slotsY); i++)
{
slots.Add (new Item());
inventory.Add (new Item ());
}
database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<ItemDatabase> ();
AddItem (0);
AddItem (1);
}
void Update()
{
if(showInventory == true)
{
Time.timeScale = 0;
GameObject.Find ("FPSController").GetComponent<FirstPersonController>().enabled = false;
}
if(showInventory == false)
{
Time.timeScale = 1;
GameObject.Find ("FPSController").GetComponent<FirstPersonController>().enabled = true;
}
if (Input.GetButtonDown("Inventory"))
{
showInventory = !showInventory;
}
}
void OnGUI()
{
tooltip = "";
GUI.skin = skin;
if(showInventory)
{
DrawInventory ();
if (showTooltip)
GUI.Box (new Rect(Event.current.mousePosition.x + 15f, Event.current.mousePosition.y, 200,100), tooltip);
}
if(draggingItem)
{
GUI.DrawTexture (new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50,50), draggedItem.itemIcon);
}
}
void DrawInventory()
{
Event e = Event.current;
int i = 0;
for (int y = 0; y < slotsY; y++)
{
for (int x = 0; x < slotsX; x++)
{
Rect slotRect = new Rect (x * 60, y * 60, 50, 50);
GUI.Box (slotRect , "", skin.GetStyle("Slot"));
slots _= inventory *;*_
_ if (slots .itemName != null)
* {
GUI.DrawTexture (slotRect, slots .itemIcon);
if (slotRect.Contains (e.mousePosition))
{
tooltip = CreateTooltip (slots );
showTooltip = true;
if (e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
{
draggingItem = true;
prevIndex = i;
draggedItem = slots ;
inventory = new Item ();
}
if (e.type == EventType.mouseUp && draggingItem)
{
inventory [prevIndex] = inventory ;
inventory = draggedItem;
draggingItem = false;
draggedItem = null;
}
}
} else if (slotRect.Contains (e.mousePosition))
{
if(e.type == EventType.mouseUp && draggingItem)
{
inventory = draggedItem;
draggingItem = false;
draggedItem = null;
}
}
if (tooltip == “”)
{
showTooltip = false;
}
i++;
}
}
}
string CreateTooltip(Item item)
{
tooltip = “<color=#ffffff>” + item.itemName + "
" + item.itemDesc;
return tooltip;
}
void RemoveItem(int id)
{
for (int i = 0; i < inventory.Count; i++)
{
if (inventory .itemID == id)
{
inventory = new Item ();
break;
}
}
}*_
* void AddItem(int id)*
* {*
* for (int i = 0; i < inventory.Count; i++)*
* {*
_ if (inventory .itemName == null)
* {
for(int j = 0; j < database.items.Count; j++)
{
if (database.items [j].itemID == id)
{
inventory = database.items [j];
}
}
break;
}
}
}
bool InventoryContains(int id)
{
bool result = false;
for(int i = 0; i < inventory.Count; i++)
{
result = inventory .itemID == id;
if (result)
{
break;
}
}
return result;
}
}
It its not finished but …
And heres my test of pickup
public class ItemPickUP : MonoBehaviour
{
public int distance;
private ItemDatabase database;
private Item item;*_
* void Start()*
* {*
* }*
* void Update ()*
* {*
* Collect ();*
* }*
* void Collect()*
* {*
* if (Input.GetKey(KeyCode.E))*
* {*
* RaycastHit hit;*
* Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);*
* if (Physics.Raycast (ray, out hit, distance))*
* {*
* if (hit.collider.gameObject.tag == “pickUP”)*
* {*
* Destroy (hit.collider.gameObject);*
* }*
* }*
* }*
* }*
}
i just want to know how to i pickUP the item and then it goes to the inventory