Void being called 4 times, no apparent reason
So, I was working on my game today, specifically the Inventory system. I have created A function to add an Item when it is found on the ground. Here's the code:
RaycastHit itemHit;
if(Physics.Raycast(new Ray(Camera.main.transform.position,Camera.main.transform.forward),out itemHit,5))
{
if(itemHit.transform.tag == "Item")
{
Debug.Log("We Are Hitting An Item!");
GUI.Label(new Rect(Screen.width/2-100,Screen.height/2,500,25),"Press INTERACT to Pickup " + itemHit.transform.GetComponent<ItemObject>().itemName);
if(Input.GetButtonDown("Interact") && inventoryContents.Count < inventoryCapacity)
{
Debug.Log("Calling AddItem!");
AddItem(itemHit.transform.GetComponent<ItemObject>().itemName);
Debug.Log("AddItem Called!");
Destroy(itemHit.transform.gameObject);
}
}
}
Now, all the code appears to work fine, But, when I press "Interact" AddItem gets called 4 times... Here's the AddItem void Code if it helps:
public void AddItem(string itemName) { if(inventoryContents.Count >= inventoryCapacity) return;
Debug.Log("Adding Item!"); ItemDatabase db = _MAIN.GetComponent(); for(int i = 0; i < db.items.Length; i++) { if(db.items[i].itemName == itemName) { inventoryContents.Add(db.items[i]); Debug.Log("Item Added!"); break; } } }
Any Help? -Kurdle
Your answer
Follow this Question
Related Questions
Display list as UI or GUI 0 Answers
inventory and item system where every item need its own use function 0 Answers
Modifying Brackeys Inventory Tutorial for Stackable Items? 0 Answers
Concept of inventory system 0 Answers
Rearranging Inventory 0 Answers