Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by john2165 · Jun 17, 2017 at 05:47 AM · uiinventoryforeachitemsstackable

Unity C# - Foreach loop - Inventory slot only changes quantity for the last child of an object

In my game, I have an inventory system. Items can get added to the inventory, and each item slot shows the quantity of an item, but I'm trying to make it that if an item is selected in the inventory (with "isOn"), and if the player uses the "Use/Equip" button, then the quantity of the item decreases by one, and the child of the item slot "text_ItemAmount" (which shows the quantity of the item) updates to show the correct quantity. However, if I have more than one items in my inventory, and I select the first item and click the "Use/Equip" button, the item decreases in quantity but it isn't shown in the game, and the "text_ItemAmount" stays to the previous quantity it was. However, If I have the very last item in the inventory selected, and I press the "Use/Equip" button, the quantity of the item decreases and the inventory slot updates it's child "text_ItemQuantity" (which shows the item's quantity in game) correctly, but this only works for the very last item, and if I try to do this for previous items, it won't set the text to the correct quantity.

Also, when the "Use/Equip" button is clicked, it says a boolean variable "use" to true.

Here is my code: (It is in the update function and will be called every frame)

 foreach (Transform child in content.transform) 
         {
             Toggle childToggle = child.GetComponent<Toggle> ();
             Text ItemSlotTextAmount = child.transform.FindChild("text_ItemAmount").GetComponent<Text>();
             BaseItem childBaseItemScript = child.GetComponent<BaseItem> ();
             if (childToggle.isOn) 
             {
                 ItemDescriptionBox.text = childBaseItemScript.ItemDescription;
                 ItemIconBox.sprite = childBaseItemScript.ItemSprite;
                 UseEquipButton.gameObject.SetActive (true);
                 if (use) 
                 { 
                     childBaseItemScript.ItemQuantity -= 1;
                     itemSlotTextAmount.text = ("(x" + childBaseItemScript.ItemQuantity + ")");
                     use = false;
                     return;
                 }
                 return;
             }
             else if (!childToggle.isOn)
             {
                 UseEquipButton.gameObject.SetActive(false);
                 ItemDescriptionBox.text = "";
                 ItemIconBox.sprite = emptyIcon;
             }
 
         }

Please tell me if I did not provide enough information to solve this or if something doesn't make sense. Thanks!

Comment
Add comment · Show 5
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
avatar image NoseKills · Jun 17, 2017 at 12:50 PM 0
Share

Do you add the click listeners to the use buttos in code or editor. If in code, how?

avatar image john2165 NoseKills · Jun 17, 2017 at 01:08 PM 0
Share

In the editor. Basically on the OnClick() function, It calls a function called "UseEquip" which sets the "use" variable to true. the UseEquip function is in the same script as the code above, which is called "InventoryListWindow"

This is the UseEquip function:

     public void UseEquip()
     {
         use = true;
     }


Here is how it looks in the editor: alt text

screenshot-2017-06-17-230500.png (47.5 kB)
avatar image NoseKills john2165 · Jun 17, 2017 at 05:25 PM 0
Share

That (second) return at the very end of if (childToggle.isOn), is it there intentionally to exit the whole method so you can't interact with items after the selected one?

I'm also not sure if you can trust the order of the objects when you foreach a transform... could an unexpected order of the items cause something in combination with that return perhaps?

avatar image sk8terboy4 · Jun 17, 2017 at 06:39 PM 0
Share

I don't know if this will help, but ins$$anonymous$$d of looping through each inventory item every frame you can use pointer events to return the selected gameobject and do your logic only once when that event is called. For example, when you click the item in the inventory, a pointer event will be called and should return the selected object then you can update your descriptions, sprite, etc. Look at PointerEventData for more info :)

avatar image toddisarockstar · Jun 18, 2017 at 01:09 AM 0
Share

i see you have put much effort into your question, but this is almost too broud to follow and could be many problems. To narrow a problem I like to use the print function in the councel. I temporarily use print before every relevant line to eli$$anonymous$$ate possibility's of a wrong input/expectation until you see what line the unexpected results are co$$anonymous$$g from. it's a good process to find exactly where the problem is.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by john2165 · Jun 18, 2017 at 04:31 AM

Thanks for the reply everyone, but I fixed the problem yesteday. turns out one of my variables (line 14) was spelt with a lowercase "i" instead of an uppercase "I".

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

109 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

Related Questions

Stacking Items in a Inventory List 2 Answers

inventory system some help and guides please :) 1 Answer

Is there a way to use the prefab image unity shows? 0 Answers

PlayFab: KeyNotFoundException: The given key was not present in the dictionary. 0 Answers

Trying to send command for non-local player. 0 Answers


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