How do I add a dropping items feature?

I’m not very great at coding, I know the basics, but nothing too technical, and I’ve been searching for a way to implement a dropping system into my 2D game. Here is what I am trying to make work: there is a pickup script that, on collision, moves this game object off the screen and calls to an add item function in my inventory script that adds a scriptable object called SO to my inventory, which is an array of scriptable objects. This inventory script takes SO and adds it to the inventory without any changes to anything else. It also sets the game object as a part of another array called picked-up weapons. If there is something in the inventory, it moves the original object back to the player to simulate the effect of the weapon being dropped. It then reassigns itself to the picked-up weapons array and removes the previous item from the inventory. If there are any questions, I will try to answer them when I can. Here is the actual code:
Pickup script:

void Update()
    {
        isAtPlayer = false;

        if (Input.GetKeyDown(KeyCode.E) && collided == true && colWithPlayer == true)
        {
            instantiatedObject = this.gameObject;
            isAtPlayer = false;
            inv.AddItem(SO);
            if (SO.weaponPriority == WeaponPriority.Special)
            {
                weaponUI.specialIcon.sprite = SO.icon;
                weaponUI.specialIcon.transform.localScale = new Vector2(1f, 1f); //use localscale instead
            }
            if (SO.weaponPriority == WeaponPriority.Main)
            {
                weaponUI.mainIcon.sprite = SO.icon;
                weaponUI.mainIcon.transform.localScale = new Vector2(1f, 1f); //use localscale instead
            }
            if (SO.weaponPriority == WeaponPriority.Side)
            {
                weaponUI.sideIcon.sprite = SO.icon;
                weaponUI.sideIcon.transform.localScale = new Vector2(1f, 1f); //use localscale instead
            }
            this.gameObject.transform.localPosition = new Vector2(30f, 3f);
        }
        if (isAtPlayer == true)
        {
            this.gameObject.transform.localPosition = player.gameObject.transform.localPosition;
        }
    }

The weapon priority just talks about which of the 3 inventory slots the item will take up and where its icon will be in the in-game inventory.
The inventory script:

public void AddItem(Weapon newItem)
    {
        int newItemIndex = (int)newItem.weaponPriority;
        if (weapons[newItemIndex] == null)
        {
            pickedUpWeapons[0] = pickup.gameObject;
        }

        if (weapons[newItemIndex] != null)
        {
            pickup.isAtPlayer = true;
            pickedUpWeapons[0] = pickup.gameObject;
            RemoveItem(newItemIndex);
        }
        weapons[newItemIndex] = newItem;
    }

Sorry if this makes no sense; I don’t really post questions to forums.

In the below section of code you ask about newItemIndex not being null and then you remove newItemIndex, and then after the null check you need newItemIndex for the weapons list. But if new item index stopped existing and was removed it couldn’t be referenced as a weapon index. See below

        if (weapons[newItemIndex] != null)
        {
            pickup.isAtPlayer = true;
            pickedUpWeapons[0] = pickup.gameObject;
            RemoveItem(newItemIndex);
        }
        weapons[newItemIndex] = newItem;

Just so you know, before you get in over your head…

These things (inventory, shop systems, character customization, crafting, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

Inventories / shop systems / character selectors all contain elements of:

  • a database of items that you may possibly possess / equip
  • a database of the items that you actually possess / equip currently
  • perhaps another database of your “storage” area at home base?
  • persistence of this information to storage between game runs
  • presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
  • interaction with items in the inventory or on the character or in the home base storage area
  • interaction with the world to get items in and out
  • dependence on asset definition (images, etc.) for presentation

Just the design choices of such a system can have a lot of complicating confounding issues, such as:

  • can you have multiple items? Is there a limit?
  • if there is an item limit, what is it? Total count? Weight? Size? Something else?
  • are those items shown individually or do they stack?
  • are coins / gems stacked but other stuff isn’t stacked?
  • do items have detailed data shown (durability, rarity, damage, etc.)?
  • can users combine items to make new items? How? Limits? Results? Messages of success/failure?
  • can users substantially modify items with other things like spells, gems, sockets, etc.?
  • does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
  • etc.

Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

Breaking down a large problem such as inventory:

https://discussions.unity.com/t/826141/4

If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

Breaking down large problems in general:

https://discussions.unity.com/t/908126/3

Thanks!