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.