How do you create a 2 slot gun system where you can pick up other guns?

Ok so Ive been using the weapon script that comes with unity where I already have the guns on my player they are just activated based on keys. I want to be able to pick up other guns and have them assigned to a slot on my player so I can switch out guns on a certain slot instead of having the same 2 guns forever. Im not sure where to start with this. I would like to have it so that all the guns are technically on the player already that way it remembers the positioning of the gun that I want.I want to be able to walk up to a “dummy” gun object on the floor which is actually just the mesh of the gun with no scripts on it(my guns shoot even if they arent attatched to the player) and have that dummy gun vanish and activate the real gun that goes along with it on whatever slot I had selected at the time. I would also want to throw a dummy mesh of the gun I swap out on the floor so I can pick it back up again. Can anyone help me implement this or show me how? I use javascript.

This is a relatively complex solution to explain but I’ll try to give you my thought process as to the method I would approach the task as I just implemented something similar the other day. I want to preface that I code in C# so my syntax might be a little off.

  1. I’d create a PlayerInventory type script that has public variables where you can drag each gun instance onto the player object. Then on Start(), I instantiate each of these gun prefabs and put their position outside of camera view while also disabling collision and their mesh renderer so that they are invisible.
  2. Create an array private var myGuns : Gameobject[] in your PlayerInventory class. The length of this array would correspond to how many “slots” you have available for the player to equip different guns. By default you’d want to fill these slots with references to the instantiated guns you created. E.g. instantiated pistol goes to mySlots[0] = myPistol
  3. You will also want to keep an active reference to what slot is currently available. Usually I just keep that information in an int or an enum (I think an enum is probably a cleaner way to approach this but for example I’ll use int because it’s simple to explain). e.g var currentActiveSlot : int;. Each time the player switches an item, update that slot with the array index that is the current active slot
  4. Now, to pick up the gun, you can do so using OnCollisionEnter() with the gun mesh that’s on the ground such as look at the gun type that you collided with, and then put that gun type that’s already instantiated onto your player into the mySlots[currentActiveSlot] (That gun mesh on the ground should have a script on it to identify what type of gun it is. You could do this via simply naming it in your scene something, but attaching a custom made GunType script would allow you to give it a name, and possibly how many bullets are in it when you pick up, etc.)
  5. Before you perform the swap, you’ll want to see what gun was previously in there, so that you can instantiate a dummy gun and then apply a rigid body to it so that it will appear to fall via gravity from your body.

There is much more to it than what I wrote, such as the need to animate the active gun out of the camera view, animating the new gun up into view, and keeping track of the amount of ammo in the different guns. But that’s the basic method that I would start to approach your question.