I want to create something to stack my items, but I don’t know how. I want to have a maximum of how much it can stack and than use a new inventoryslot.
This is my code:
Scriptable Item code:
using UnityEngine;
[CreateAssetMenu(fileName = "new item", menuName = "Inventory/Item")]
public class Item : ScriptableObject {
new public string name = "new item";
public Sprite icon = null;
public GameObject objectToDrop;
public string description;
public int maxStackSize;
public bool isStackable;
}
The InventorySlot script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using System.Collections;
using TMPro;
public class InventorySlot : MonoBehaviour {
public Image icon, icon2;
public Button removeButton;
public Transform player;
public GameObject uiDescription, sellItem, nextStage;
public TextMeshProUGUI textDescription, itemNameText;
private Item item;
private void Start()
{
icon.enabled = false;
textDescription = textDescription.GetComponent<TextMeshProUGUI>();
itemNameText = itemNameText.GetComponent<TextMeshProUGUI>();
}
private void Update()
{
if (icon.sprite != null)
{
icon.enabled = true;
}
else {
icon.enabled = false;
}
}
public void AddItem(Item newItem) {
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
removeButton.GetComponent<Image>().enabled = true;
}
public void ClearSlot() {
icon.sprite = null;
icon.enabled = false;
removeButton.GetComponent<Image>().enabled = false;
}
public void OnRemoveButton() {
PlayerInventory.instance.Remove(item);
Instantiate(item.objectToDrop, player.transform.position, Quaternion.identity);
uiDescription.SetActive(false);
item = null;
}
public void UIOpenButton() {
if (item != null) {
uiDescription.SetActive(!uiDescription.activeSelf);
icon2.sprite = item.icon;
textDescription.text = item.description;
itemNameText.text = item.name;
}
}
}
The PlayerInventory script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerInventory : MonoBehaviour {
public GameObject inventory, escapeMenu, playerUIStats, playerSelflookInventory;
public int inventorySpace;
public List<Item> items = new List<Item>();
public bool inventoryAcces;
private bool inventoryClose;
#region Singleton
public static PlayerInventory instance;
private void Awake()
{
if (instance != null) {
Debug.LogWarning("More then one inventory");
return;
}
instance = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallBack;
private void Start()
{
inventoryAcces = true;
}
// Update is called once per frame
void Update () {
KeyInput();
Inventory();
}
#region Inventory- &EscapeClose/Open
public bool Add(Item item) {
if (items.Count >= inventorySpace) {
Debug.Log("not enough space to pickup");
return false;
}
items.Add(item);
if (onItemChangedCallBack != null)
{
onItemChangedCallBack.Invoke();
}
return true;
}
public void Remove(Item item) {
items.Remove(item);
if (onItemChangedCallBack != null)
{
onItemChangedCallBack.Invoke();
}
}
}
(I have deleted some code in the PlayerInventory script about opening and closing the inventory, because I think it isn’t needed for this problem. If you need to see it please say it).
The inventoryUI updating script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryUI : MonoBehaviour {
public Transform itemsParent;
private PlayerInventory inventory;
private InventorySlot[] slots;
void Start () {
inventory = PlayerInventory.instance;
inventory.onItemChangedCallBack += UpdateUI;
slots = itemsParent.GetComponentsInChildren<InventorySlot>();
}
private void UpdateUI()
{
for (int i = 0; i < slots.Length; i++)
{
if (i < inventory.items.Count)
{
slots[i].AddItem(inventory.items[i]);
}
else
{
slots[i].ClearSlot();
}
}
}
}
If someone knows how to create the stacking system please let me know. I’m struggeling with it for almost a week now.
(I also have a bug with dropping a item and then pick it up, but when I pick it up it changed to a different item. I think I can do that by my own, but I have found the bug just a hour ago. It’s not my main question, but if you know what is causing that bug please let me know).
Thanks for helping.