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 /
  • Help Room /
avatar image
0
Question by fluxhackspro · Feb 20, 2018 at 03:38 PM · listremove

Unity Glitch or am i calling functions twice? List.Remove problem

Hello so i followed brackeys RPG turorial just to see how he did it. When i simple understood how he did it i left hist tutorial and did it myself (using his great sprites and assets). Simply to learn. But somehow when i equip an item it disapears in the UI so the "UpdateUI" function is calling, and the "ClearSlot" too. But in the editor the item that is suppose to be removed is still in the Item List List<Item> items = new List<Item>();

Then if i remove an item or drop it, and add it again to the inventory or simplier when i call the UpdateUI function it appears again. But i have it equiped so it duplicates.

The problem lies on the inventory script in the Remove() funciton i clearly say items.Remove(item) But it doesn't remove the item???

Any help is welcome Ps. Item is a scriptable object with only a few variables!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Inventory : MonoBehaviour {
 
     public GameObject inventoryPanel;
     public int maxSlots;
     public GameObject dropItem;
 
     public List<Item> items = new List<Item>();
 
     bool showPanel = false;
     InventoryUI ui;
 
     void Start ()
     {
         ui = GameObject.FindObjectOfType<InventoryUI> ();
     }
 
     void Update ()
     {
         //Toggle inventory
         if (Input.GetKeyDown (KeyCode.I)) {
             showPanel =! showPanel;
         }
 
         if (showPanel == true) {
             inventoryPanel.SetActive (true);
         } else {
             inventoryPanel.SetActive (false);
         }
     }
 
     public void Add (Item item)
     {
         items.Add (item);
         ui.UpdateUI ();
         //Debug.Log ("Added " + item.name + " to inventory");
     }
 
     public void Remove (Item item, Mesh mesh, Material mat, float size, bool spawnObject)
     {
         items.Remove (item); //THIS LINE OF CODE IS NOT BEING EXECUTED THE FIRST TIME YOU CALL THIS FUNCTION. BUT AFTER YOU CALL UpdateUI () IT DOES???
         Debug.Log ("Remove " + item + " PLEASE");
         ui.UpdateUI ();
         items.Remove (item);
 
         Vector3 offset = new Vector3 (0, 2, 2);
 
         if (spawnObject == true) {
             //My spin on dropping item
             GameObject droppedItem = Instantiate(dropItem) as GameObject;
             droppedItem.transform.position = transform.position + offset;
             droppedItem.GetComponent<PickUpItem>().item = item;
             droppedItem.GetComponent<PickUpItem> ().UpdateModel (mesh, mat, size);
         }
 
     }
 }
 

 using UnityEngine;
 using UnityEngine.UI;
 
 public class InventorySlot : MonoBehaviour {
 
     public Image icon;
 
     public Button Remove;
 
     Item item;
     Mesh mesh;
     Material mat;
     float size;
     Inventory inv;
     Equipment equip;
 
     void Start ()
     {
         equip = GameObject.FindObjectOfType<Equipment> ();
         inv = GameObject.FindObjectOfType<Inventory> ();
     }
 
     public void AddItem (Item newItem, Mesh newMesh, Material newMat, float newSize)
     {
         item = newItem;
         mesh = newMesh;
         mat = newMat;
         size = newSize;
 
         icon.GetComponent<Button>().interactable = true;
         Remove.interactable = true;
         icon.enabled = true;
         icon.sprite = item.icon;
     }
 
     public void ClearSlot ()
     {
         Debug.Log ("Cleared Slots");
         item = null;
         Remove.interactable = false;
         icon.sprite = null;
         icon.GetComponent<Button>().interactable = false;
 
         icon.enabled = false;
     }
 
     public void OnRemoveButton ()
     {
         inv.Remove (item, mesh, mat, size, true);
         ClearSlot ();
     }
 
     public void OnEquip ()
     {
         if (item.equipment == true)
         {
             equip.EquipItem (item.index, item.type, mesh, mat, size);
             ClearSlot ();
         }
         else
         {
             //THIS IS NOT SOMETHING YOU CAN EQUIP 
             ///PUT YOUR CODE HERE
             Debug.Log("Not Equipment");
         }
     }
 }


using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Equipment : MonoBehaviour {

 public GameObject[] goEquipment;

 public int[] currentEquipment;
 public Item[] items;

 int index;

 public void EquipItem (int newIndex, int type, Mesh mesh, Material mat, float size) //0 helmet, 1 chestplate, 2 greaves
 {
     if (currentEquipment [type] != 0)
     {
         goEquipment [type].SetActive (false);
         goEquipment [newIndex].SetActive (true);
         currentEquipment[type] = newIndex;
     }
     else if(currentEquipment [type] == 0) 
     {
         currentEquipment[type] = newIndex;
         goEquipment [newIndex].SetActive (true);
     }

     GetComponent<Inventory>().Remove (items[index], mesh, mat, size, false);

     index = newIndex;
 }

}

 using UnityEngine;
 
 public class InventoryUI : MonoBehaviour {
 
     public Transform itemsParent;
 
     Inventory inv;
     InventorySlot[] slots;
 
     void Start ()
     {
         inv = GameObject.FindObjectOfType<Inventory> ();
         slots = itemsParent.GetComponentsInChildren<InventorySlot> ();
     }
 
     public void UpdateUI ()
     {
         //Debug.Log ("Update UI");
 
         for (int i = 0; i < slots.Length; i++) {
             if (i < inv.items.Count) {
                 slots [i].AddItem (inv.items [i], inv.items[i].model, inv.items[i].mat, inv.items[i].modelSize);
             } else {
                 slots [i].ClearSlot ();
             }
         }
     }
 }
 




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.

Follow this Question

Answers Answers and Comments

76 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

Related Questions

Why is my listdisplaying that an item was removed? 0 Answers

Problem with removing an item from a list 0 Answers

Problem with clearing list 0 Answers

Problem with Lists and Remove 0 Answers

Remove and RemoveAt for List not working 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