• 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 MrPanda05 · Oct 23, 2022 at 07:12 PM · listinventoryinventory systemremoteitem

Removing item from inventory not updating correct based on order

I'm trying to do a inventory system, the way it works is that I have an empty gamebject that holds in a list all items that I have collected and on my UI, I have a object that holds 20 prefabs that serve as a container for t$$anonymous$$s item on that list. When the inventory is empty, all prefabs are deactivated, they are activated based on the items on the list. Now i'm trying to delete a certain item, but it only works if I go form the $$anonymous$$ghest index to the lowest, if I do the lowest or a middle index, it does not work.

Edit1:My hypotesis is that when I remove an item, the second index will became the new first, so it cause an error in logic, but I just don't know how to fix.

My script that handles updating the UI

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 using UnityEngine.EventSystems;
 using TMPro;
 using UnityEngine.UI;
 public class TestLoop : MonoBehaviour
 {
     public int num;
 
 
     public Inventory inventory;
     public GameObject[] holder;
     public Button[] SpecialButtons;
 
     public GameObject Selector;
 
     public bool isOnIventory;
 
     public bool inDelete;
 
     private void OnEnable()
     {
         isOnIventory = true;
     }
     private void OnDisable()
     {
         isOnIventory = false;
     }
     private void Start()
     {
         //Sets all slots to be deactivated
         for (int i = 0; i < holder.Length; i++)
         {
             holder[i].SetActive(false);
         }
     }
     //item.GetComponentInC$$anonymous$$ldren<TextMeshProUGUI>().text = item.item.displayName;
 
     //When Iventory slot is clicked call t$$anonymous$$s
     public void GoToBottom()
     {
         num = Array.IndexOf(holder, EventSystem.current.currentSelectedGameObject);
         Selector.transform.position = SpecialButtons[0].transform.position;
         SpecialButtons[0].Select();
         print(num);
         inDelete = true;
     }
 
     //Test to delete Item
     public void TestDelete()
     {
         if (inventory.inventory[num] == null)
         {
             return;
         }
         else
         {
             if (num < 0)
             {
                 return;
             }
             holder[num].SetActive(false);
             inventory.inventory.Remove(inventory.inventory[num]);
             inDelete = false;
         }
     }
 
     //Update Item UI
     void UpdateItens()
     {
         if (inventory.inventory.Count < holder.Length)
         {
             for (int i = 0; i < inventory.inventory.Count; i++)
             {
                 holder[i].SetActive(true);
                 var bruh = inventory.inventory[i].itemData;
                 holder[i].GetComponentInC$$anonymous$$ldren<TextMeshProUGUI>().text = bruh.displayName;
             }
         }
     }
 
     private void Update()
     {
         print(num);
         if (inDelete)
         {
 
         }
         else
         {
             UpdateItens();
         }
     }
 }
 
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

1 Reply

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

Answer by MrPanda05 · Oct 23, 2022 at 08:52 PM

I kinda fixed by putting the code in the start in a another function and calling that after

 inventory.inventory.Remove(inventory.inventory[num]);
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

148 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

Related Questions

How i can make a script-made button interactable? 0 Answers

[C#]Inventory script help. 3 Answers

Problem with dropping items from my inventory 0 Answers

Hey, i can't figure out, what my problem is. I want to Add something into the player's Inventory, but before that it should check, whether there is an Item with the same name in it, or not and if yes, if the maxAmount for that slot is reached. 0 Answers

inventory system: drop item with mouseclick 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