For Loop Outputting Double
Hello! I'm working on an inventory and crafting system in Unity, and the for loop for removing items after crafting is removing twice what I want it to. How can I fix this? Here's my code (public void RemoveItem() is the important bit): using System; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEditor; using UnityEditorInternal; using UnityEngine; public class Button : MonoBehaviour { public Crafting recipe; public TextMeshProUGUI descriptionText; public GameObject craftButton; public Transform craftButtonPos; public void OnButtonClick() { var craftButtonVar = Instantiate(craftButton, craftButtonPos.position, craftButtonPos.rotation); craftButtonVar.transform.SetParent(transform); descriptionText.text = recipe.description; } public void OnCraftButton() { for (int i = 0; i < recipe.playerInventory.Container.Items.Length; i++) { for (int e = 0; e < recipe.neededItems.Length; e++) { if (recipe.playerInventory.Container.Items[i].ID == recipe.neededItems[e].item.id && recipe.playerInventory.Container.Items[i].amount >= recipe.neededItems[e].amount) { recipe.neededItems[e].has = true; } } } if (CanCraft()) { recipe.playerInventory.AddItem(new Item(recipe.item), recipe.amountGiven); for (int i = 0; i < recipe.playerInventory.Container.Items.Length; i++) { for (int e = 0; e < recipe.neededItems.Length; e++) { if (recipe.playerInventory.Container.Items[i].item.Name == recipe.neededItems[e].item.name && recipe.playerInventory.Container.Items[i].amount >= recipe.neededItems[e].amount) { RemoveItem(recipe.playerInventory.Container.Items[i].item); //maybe this recipe.neededItems[e].has = false; } } } } } private bool CanCraft() { for (int i = 0; i < recipe.neededItems.Length; ++i) { if (recipe.neededItems[i].has == false) { return false; } } return true; } public void RemoveItem(Item _item) { for (int i = 0; i < recipe.playerInventory.Container.Items.Length; i++) { for (int e = 0; e < recipe.neededItems.Length; e++) { if (recipe.playerInventory.Container.Items[i].item.Name == recipe.neededItems[e].item.name && recipe.playerInventory.Container.Items[i].amount >= recipe.neededItems[e].amount) { recipe.playerInventory.Container.Items[i].AddAmount(-recipe.neededItems[e].amount); } } } } }
Your answer
Follow this Question
Related Questions
How to convert object types? 0 Answers
For loop not looping, Conditional break and return question? 0 Answers
Crafting help checking for more than one material 3 Answers
[Help] I don't know how to create a stacking system for my inventory! 3 Answers
How can i loop through stack and take a set number from each? 0 Answers