• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by FlamingFox1307 · Jun 22, 2020 at 03:02 PM · inventoryfor-loopcrafting

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); } } } } }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

For Loop Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges