Inventory problem

Hi, i have some questions and problems about inventory system in unity. First of all, i created some sort of inventory using youtube channels but, when i pick one item two times, it will take two slots as well. For example, if i pick Two swords, it will take two slots but i don’t like it this way. can someone help me? i have this pickup code.

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

public class pickup : MonoBehaviour
{
    private Inventory inventory;
    private InventoryTwo invenven;
    public GameObject itemButton;
    void Start()
    {
        inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
        invenven = GameObject.FindGameObjectWithTag("Player").GetComponent<InventoryTwo>();
    }
    void OnTriggerEnter2D(Collider2D Other)
    {
        if (Other.CompareTag("Player"))
        {
            for (int i = 0; i < inventory.slots.Length; i++)
            {
                if (inventory.isFull[i] == false)
                {
                    inventory.isFull[i] = true;
                    Instantiate(itemButton, inventory.slots[i].transform, false);
                    Destroy(gameObject);
                    break;
                }
            }
        }
        if (Other.CompareTag("Player"))
        {
            for (int i = 0; i < invenven.slots.Length; i++)
            {
                if (invenven.isFull[i] == false)
                {
                    invenven.isFull[i] = true;
                    Instantiate(itemButton, invenven.slots[i].transform, false);
                    Destroy(gameObject);
                    break;
                }
            }
        }
    }
}

My second question is that how can i make a select part for these. for exampe, if I Scroll down to First slot, my character pick the Item that is in it? like stardew valley!

Hi @csiroid

How is this a 2D question IMO this is more like a general scripting question…

Also, I don’t see much of inventory structure, you actually are showing your pickup script, not inventory…

So I’m guessing.

I would create a container item for inventory items. It will only hold information about picked item. When you pickup item you only store its name or id in inventory item. Maybe you already have an item class…

When you drop item you spawn it by its id stored in inventory item. This way you can also have other properties in your inventory item, like stacking which is what you are asking. If item is “stackable” you can simply add to / remove from its stack counter. So each time you add item, first query your inventory, if it already has similar item with a stack that isn’t max stack value, then add to it, otherwise add a new item.

“My second question is that how can i make a select part for these. for exampe, if I Scroll down to First slot, my character pick the Item that is in it? like stardew valley!”

You are asking several questions. Maybe try to first create your systems yourself and then if you get stuck ask help again?