- Home /
Equipment Slots not completely working
I made some progress on getting my equipment slots to work, but not completely. Can't seem to figure this part out. At runtime, the (blank) item icons are active, and if I equip any item, they go inactive. Except there are 2 items that show up on ALL of the slots. See in the video: https://youtu.be/MhbSX2S9R-U
Here is my equipment slot code:
using UnityEngine; using UnityEngine.UI;
public class EquipSlot : MonoBehaviour {
public EquipmentSlot equipmentSlot;
EquipUI UI;
public Equipment equipment;
public Image icon;
public Button removeButton;
Item item;
public bool isEquipped;
public void Start()
{
EquipmentManager.instance.onEquipmentChanged += OnEquipmentChanged;
}
public void EquipItem(Equipment equipment)
{
item = equipment;
icon.sprite = item.icon;
icon.enabled = true;
this.equipment = equipment; //reference to equipped item
removeButton.interactable = true;
}
public void ClearSlot()
{
item = null;
icon.sprite = null;
icon.enabled = false;
removeButton.interactable = false;
}
public void OnRemoveButton()
{
//Remove from equipment slot
}
public void OnEquipmentChanged(Equipment newItem, Equipment oldItem) { if (newItem != null) { item = equipment;
icon.sprite = newItem.icon;
icon.enabled = true;
removeButton.interactable = true;
//this.equipment = equipment;
}
if (oldItem != null)
{
item = null;
icon.sprite = null;
icon.enabled = false;
removeButton.interactable = false;
}
}
}
Equipment Manager:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EquipmentManager : MonoBehaviour {
#region Singleton
public static EquipmentManager instance;
private void Awake()
{
instance = this;
}
#endregion
public List<Equipment> items = new List<Equipment>();
public Equipment[] defaultItems;
public SkinnedMeshRenderer targetMesh;
public Equipment[] currentEquipment; //Items we currently have equipped
SkinnedMeshRenderer[] currentMeshes;
//Callback for when an item is equipped/unequipped
public delegate void OnEquipmentChanged(Equipment newItem, Equipment oldItem);
public OnEquipmentChanged onEquipmentChanged;
Inventory inventory; //Ref to inventory
private void Start()
{
inventory = Inventory.instance; //Get ref to inventory
//Initialize currentEquipment based on number of equipment slots
int numSlots = System.Enum.GetNames(typeof(EquipmentSlot)).Length;
currentEquipment = new Equipment[numSlots];
currentMeshes = new SkinnedMeshRenderer[numSlots];
EquipDefaultItems();
}
//Equip a new item
public void Equip (Equipment newItem)
{
//Find out what slot the item fits in
int slotIndex = (int)newItem.equipSlot;
Equipment oldItem = Unequip(slotIndex);
//An item has been equipped so we trigger the callback
if(onEquipmentChanged != null)
{
onEquipmentChanged.Invoke(newItem, oldItem);
}
//Insert the item into the slot
currentEquipment[slotIndex] = newItem;
SkinnedMeshRenderer newMesh = Instantiate<SkinnedMeshRenderer>(newItem.mesh);
newMesh.transform.parent = targetMesh.transform;
newMesh.bones = targetMesh.bones;
newMesh.rootBone = targetMesh.rootBone;
currentMeshes[slotIndex] = newMesh;
}
//Unequip an item with a particular index
public Equipment Unequip (int slotIndex)
{
//Only do this if an item is there
if (currentEquipment [slotIndex] != null)
{
if(currentMeshes[slotIndex] != null)
{
Destroy(currentMeshes[slotIndex].gameObject);
}
//Add the item to the inventory
Equipment oldItem = currentEquipment[slotIndex];
inventory.Add(oldItem);
//Remove the item from the equipment array
currentEquipment[slotIndex] = null;
//Equipment has been removed to we trigger the callback
if (onEquipmentChanged != null)
{
onEquipmentChanged.Invoke(null, oldItem);
}
return oldItem;
}
return null;
}
public void UnequipAll()
{
for (int i = 0; i < currentEquipment.Length; i++)
{
Unequip(i);
}
EquipDefaultItems();
}
void EquipDefaultItems()
{
foreach(Equipment item in defaultItems)
{
Equip(item);
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.U))
UnequipAll();
}
}
EquipUI:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EquipUI : MonoBehaviour {
public Transform EquipSlots;
Inventory inventory;
EquipmentManager manager;
public EquipSlot slot;
EquipSlot[] eSlots;
// Use this for initialization
void Start () {
manager = EquipmentManager.instance;
//manager.onEquipmentChanged += UpdateUI;
eSlots = EquipSlots.GetComponentsInChildren<EquipSlot>();
}
// Update is called once per frame
void Update () {
}
void UpdateUI()
{
for (int i = 0; i < eSlots.Length; i++)
{
if(i < manager.items.Count)
{
eSlots[i].EquipItem(manager.items[i]);
} else
{
eSlots[i].ClearSlot();
}
}
}
}
Your answer
Follow this Question
Related Questions
I need to find inventory and item system, and to save them 0 Answers
Database solution networking 0 Answers
What is a good component to use for a GUI inventory display? 1 Answer
Iterating a GO through an array of transform positions 0 Answers
Inventory System Using InScope Studios Tutorial Help 2 Answers