My Inventory system is in a big mess now, help me, please
I'm currently creating inventory system for my game. the general concept is, the inventory has room for 5 items and its UI will always be at the bottom of the screen, so no enable/disable function, it's always there. when the player picks an item, it goes to the inventory, first slot first. and if the player pick up another item and the first slot is occupied, the new item goes to the second slot.
when there is an item in a slot, if you press a number key(Alpha1-Alpha5 for Slot0-Slot4) corresponding to the slot, it will make the slot goes "active". for example, if you have an axe in the first slot and you press 1, slot0 will be now "active", thus sending the axe to the player's hand via EquipmentManager script.(from this point in the game, the player will be seen holding an axe). the axe will still stay in the slot though. and when press 1 again, Slot0 will goes "inactive", the axe is no longer equipped in the player's hand. If there is another item like a sword in Slot1 and you press 2, items will be switched, the axe goes inactive(into its slot, the Slot0) and the sword goes into the hand instead.
every item in the inventory will have its fixed slot position until it is dropped off, then the item variable "inWhichSlot" will reset to 0.
the problem is, first of all, when the game is started, there'll always telling me that "more than one instance of Inventory is detected" and the InventorySlot[] array in the InventoryUI script always detect 10 slot components(Slot0-Slot9, there should be only 5 of them) and when i picked up an item, the item goes to the first slot(Slot0) normally and then it cleared out Slot1-Slot9, leaving no icon on the screen at all, there is nothing shown on the ItemList<> or currentItemInInventory[] array in the Inventory script, they're both shown as blanks.
when I press Alpha1 key to get Slot0 activated, the item in the slot duplicated itself until it fills all the slot, the item's icon appeared on the Slot0 and Slot1's UI(only two of the five slots) and if I get to pick up another item, it will say that the Inventory is full. in the debug.log, it's furiously Equip and Unequip the item in a loop endlessly.
this is basically what happened with my code now, it's a total mess. hope that anyone has the solution to this hideous situation, haha. if you need my Unity file or my Script files you can tell me.
Thank you so much in advance for anyone answering and also I have to apologize in advance, too. for my code. It's going to cause a lot of headache for sure. I'm very much in desperation now, but not giving up
here is my code
public class Inventory : MonoBehaviour { #region Singleton public static Inventory instance;
private void Awake()
{
if (instance != null)
{
Debug.LogWarning("More than one instance of inventory founded ");
return;
}
instance = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
public int space=5;
public List<Items> itemList = new List<Items>();
public Items[] currentItemInInventory;
private void Start()
{
currentItemInInventory = new Items[space];
}
public bool Add(Items item,int slotIndex)
{
if (itemList.Count >= space)
{
Debug.Log("Not enough room");
return false;
}
itemList.Add(item);
currentItemInInventory[slotIndex] = item;
item.inWhichSlot = slotIndex;
//this code updates the inventory
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
return true;
}
public void Remove(Items item,int slotIndex)
{
Debug.Log("Removing item from the list");
itemList.Remove(item);
item.inWhichSlot = 0;
currentItemInInventory[slotIndex] = null;
if (onItemChangedCallback != null)
{
onItemChangedCallback.Invoke();
}
}
}
public class InventoryUI : PlayerUIScript { public Transform slotHolder; Inventory inventory;
public InventorySlot[] slots;
void Start()
{
inventory = Inventory.instance;
inventory.onItemChangedCallback += UpdateUI;
slots = slotHolder.GetComponentsInChildren<InventorySlot>();
Debug.Log("there are " + slots.Length + " slot components detected");
}
void UpdateUI()
{
Debug.Log("UPDATING UI");
for (int i = 0; i < slots.Length; i++)
{
if (i < inventory.itemList.Count)
{
slots[i].AddItem(inventory.itemList[i]);
Debug.Log("item " + inventory.itemList[i] + " added into inventory No."+i);
}
else
{
slots[i].ClearSlot();
Debug.Log("Clearing slot No."+i);
}
}
}
private void Update()
{
SlotKeyActivation();
}
private void SlotKeyActivation()
{//this code basically make the slot goes activated when pressed the Alpha number corresponding to the slot and also deactivated other slots if they're previously activated, pretty embarrased by its clunkiness
if (Input.GetKeyDown(KeyCode.Alpha1))
{
slotOn1 = !slotOn1;
//these code a little bit clunky
if (slotOn1 == true)
{
slotOn2 = false;
slotOn3 = false;
slotOn4 = false;
slotOn5 = false;
}
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
slotOn2 = !slotOn2;
if (slotOn2 == true)
{
slotOn1 = false;
slotOn3 = false;
slotOn4 = false;
slotOn5 = false;
}
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
slotOn3 = !slotOn3;
if (slotOn3 == true)
{
slotOn1 = false;
slotOn2 = false;
slotOn4 = false;
slotOn5 = false;
}
}
if (Input.GetKeyDown(KeyCode.Alpha4))
{
slotOn4 = !slotOn4;
if (slotOn4 == true)
{
slotOn1 = false;
slotOn2 = false;
slotOn3 = false;
slotOn5 = false;
}
}
if (Input.GetKeyDown(KeyCode.Alpha5))
{
slotOn5 = !slotOn5;
if (slotOn5 == true)
{
slotOn1 = false;
slotOn2 = false;
slotOn3 = false;
slotOn4 = false;
}
}
if (Input.GetKeyDown(KeyCode.X))
{
slotOn1 = false;
slotOn2 = false;
slotOn3 = false;
slotOn4 = false;
slotOn5 = false;
}
}
}
public class EquipmentManager : MonoBehaviour { //this script controls which item should be on the player's hand #region Singleton public static EquipmentManager instance; void Awake() { instance = this; } #endregion Items currentEquipment; Inventory inventory; public Transform grab;
public delegate void OnEquipmentChanged(Items itemInHand, Items oldItem);
public OnEquipmentChanged onEquipmentChanged;
void Start()
{
inventory = Inventory.instance;
}
public void Equip(Items itemInHand)
{
//means get the item to hand
Items oldItem = null;
if (currentEquipment != null)
{
oldItem = currentEquipment;
inventory.Add(oldItem,oldItem.inWhichSlot);
}
currentEquipment = itemInHand;
if (onEquipmentChanged != null)
{
onEquipmentChanged.Invoke(itemInHand, oldItem);
}
Debug.Log("item has been placed on hand");
}
public void Unequip()
{
if (currentEquipment != null)
{
Debug.Log("UnEqipping " + currentEquipment);
Items oldItem = currentEquipment;
inventory.Add(oldItem,oldItem.inWhichSlot);
currentEquipment = null;
if (onEquipmentChanged != null)
{
onEquipmentChanged.Invoke(null, oldItem);
}
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.X))
{
Unequip();
}
}
}
public class InventorySlot : MonoBehaviour { // this is a script for each slots, I have five of them in each slot gameObject
EquipmentManager equipManager;
public Image icon;
public Items itemInDaSlot;
public GameObject SlotYellow;
public bool thisSlotIsActive = false;
public int slotIndex;
private void Start()
{
equipManager = EquipmentManager.instance;
}
public void AddItem(Items newItem)
{
itemInDaSlot = newItem;
icon.sprite = itemInDaSlot.icon;
icon.enabled = true;
}
public void ClearSlot()
{
itemInDaSlot = null;
icon.sprite = null;
icon.enabled = false;
}
public void Update()
{
if (SlotYellow.activeSelf == true)
{ thisSlotIsActive = true; }
else { thisSlotIsActive = false; }
//automatically equip an item if this slot is active
if (thisSlotIsActive == true && itemInDaSlot!=null) { equipManager.Equip(itemInDaSlot); }
else if (thisSlotIsActive == false && itemInDaSlot!=null) { equipManager.Unequip(); }
}
}
public class ItemPickup : Interactables { public Items item;
public override void Interact()
{
base.Interact();
PickUp();
}
void PickUp()
{
Debug.Log("Picking up item ="+ item.name);
bool wasPickedUp = Inventory.instance.Add(item,0);
//I input the number for the first slot(number 0) because I was going to make a code in the Add function to check if the slot is occupied, if it is, the number will goes up by 1 to see the next slot space to put the picked up item on, but i haven't succeed to come up with the code yet
if (wasPickedUp)
{
gameObject.SetActive(false);
}
}
}
Your answer
Follow this Question
Related Questions
Adding a prefab to the Item class instance 0 Answers
How Can I make items inventory? (HELP) 0 Answers
IPointerEnterHandler and IPointerExitHandler not working 1 Answer
Inventory Master 0 Answers