- Home /
Question by
ZaneBrown · Jan 26 at 01:24 PM ·
buglistsfirst-person-controllerinventory system
Issue with equipping object
When picking up an object, they are automatically added to the inventory. I can correctly add the item to my hand when I press "1". However, when I press "2" the second item stacks on top of the first item.
However, when I press "1" again, the second item disappears correctly and when I press "2" again, the first item disappears correctly.
The problem is that the first item will not disappear correctly when I press "2" for the first time. It only works correctly when I press "1" twice.
What exactly is the issue here? I've tried deactivating the object when another button is pressed, but I get the same result. Here's some of the code I am talking about.
Select Items to be Equipped
void SelectItems()
{
foreach (GameObject item in items)
{
if (Input.GetKeyDown(KeyCode.Alpha1) && items.Count >= 1)
{
items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
itemToBeEquipped = 0;
items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
Debug.Log("Item 1 Equipped");
}
else if (Input.GetKeyDown(KeyCode.Alpha2) && items.Count >= 2)
{
items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
itemToBeEquipped = 1;
items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
Debug.Log("Item 2 Equipped");
}
else if (Input.GetKeyDown(KeyCode.Alpha3) && items.Count >= 3)
{
items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
itemToBeEquipped = 2;
items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
Debug.Log("Item 3 Equipped");
}
else if (Input.GetKeyDown(KeyCode.Alpha4) && items.Count >= 4)
{
items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
itemToBeEquipped = 3;
items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
Debug.Log("Item 4 Equipped");
}
else if (Input.GetKeyDown(KeyCode.Alpha5) && items.Count == 5)
{
items[itemToBeEquipped].GetComponent<ItemData>().UnequipItem();
itemToBeEquipped = 4;
items[itemToBeEquipped].GetComponent<ItemData>().EquipItem();
Debug.Log("Item 5 Equipped");
}
}
}
PickUpItem Method
public void PickUpItem()
{
if (InventoryScript.items.Count < 5)
{
//disable picked up item colliders and rigidbody
coll1.enabled = false;
coll2.enabled = false;
rb.isKinematic = true;
gameObject.transform.parent = pos_pickedUpItem.transform;
gameObject.transform.position = pos_pickedUpItem.position;
isInInventory = true;
isEquipped = true;
InventoryScript.itemToBeEquipped = InventoryScript.items.Count;
InventoryScript.items.Add(gameObject);
EquipItem();
gameObject.GetComponent<MeshRenderer>().enabled = false;
//Debug.Log("Picked up " + str_ItemName + "!");
//Debug.Log("There are " + InventoryScript.items.Count + " in your inventory");
}
else if (InventoryScript.items.Count == 5)
{
Debug.Log("Error: Not enough space to pick up this item!");
}
}
Equip / Unequip Method
public void EquipItem()
{
gameObject.GetComponent<MeshRenderer>().enabled = true;
isEquipped = true;
isActivated = true;
//gameObject.SetActive(true);
//Debug.Log("Equipped " + str_ItemName + "!");
}
public void UnequipItem()
{
gameObject.GetComponent<MeshRenderer>().enabled = false;
isEquipped = false;
isActivated = false;
//gameObject.SetActive(false);
//Debug.Log("Unequipped " + str_ItemName + "!");
}
Comment