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 ();
}
}
}
}
Your answer
Follow this Question
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