Switching Weapons (C#)

I know the logic that I want to implement, but I don’t know how to actually program it. What I want to do is have it so that if you hit one of the alpha buttons (1, 2, 3) the script will enable the corresponding object in an array. Then I want to disable ever other object in the array. This way the number of weapons in the inventory can be changed. It would be really helpful for an explanation of a script as well. Thanks!

Make your List in your inventory script

using System.Collections.Generic;

private List<GameObject> weaponsList;

Then assign the Weapons you have to it so that after you pick up a weapon it goes into your inventory.

//On pick up of sword
inventory.weaponsList.Add(//gameobject here like sword)

Then after you equip the weapon you want like “sword” set it to the variable “currentWeapon”

foreach(GameObject weapon in inventory.weaponsList)
{
    if(gameObject.activeSelf)
    {
       gameObject.SetActive(false);
    }
}

//Now that they're all off you turn yours back on
currentWeapon.SetActive(true);

Now I know this is kinda rough but this is how I would start it.

You can implement a Weapon class. Then have all your weapons inherit from it.
From there you can create a Weapon array that contains all your weapons.
So to sum up the class list should look like this :

Class Weapon
Class Rifle : Weapon
Class Shotgun : Weapon
etc.

Of course every method you call through the Weapon array needs to be defined in the Weapon class (as abstract or virtual) and the specific weapon (rifle, shotgun, whatnot) should override it if need be.

In essence, that’s polymorphism at work.

I hope I was clear.