- Home /
Question by
Kirbsssys · Oct 05, 2021 at 11:14 PM ·
collideritemgetkeydowndrop
I want to know how to drop my item with a keyboard button
I want to be able to remove the items from my inventory when I push Q, this is the code for picking up the items
public class PlayerInventory : MonoBehaviour
{
public InventoryObject inventory;
public void OnTriggerEnter(Collider other)
{
var item = other.GetComponent<Item>();
if (item)
{
inventory.AddItem(item.item, 1);
Destroy(other.gameObject);
}
}
}
and this is how the code is adding the items
[CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory System/Inventory")]
public class InventoryObject : ScriptableObject
{
public List<InventorySlot> Container = new List<InventorySlot>();
public void AddItem(ItemObject _item, int _amount)
{
bool hasItem = false;
for (int i = 0; i < Container.Count; i++)
{
if(Container[i].item == _item)
{
Container[i].AddAmount(_amount);
hasItem = true;
break;
}
}
if (!hasItem)
{
Container.Add(new InventorySlot(_item, _amount));
}
}
}
[System.Serializable]
public class InventorySlot
{
public ItemObject item;
public int amount;
public InventorySlot(ItemObject _item, int _amount)
{
item = _item;
amount = _amount;
}
public void AddAmount(int value)
{
amount += value;
}
}
Comment
Answer by OlleStarclassic · Oct 06, 2021 at 12:45 AM
Hello :)
Code not tested.
In PlayerInventory class:
public void Update()
{
if (Input.GetKey("Q"))
{
inventory.RemoveItems();
}
}
In inventoryObject class:
public void RemoveItems()
{
Container = new List<InventorySlot>();
}
Your answer
Follow this Question
Related Questions
Pickup item and move with it -1 Answers
Item drop on monster kill 1 Answer
Internal collisions 1 Answer
Random drop rate problem 2 Answers