How to limit my weapon switch script so that way it only works on weapons that are bought?

Currently, I am trying to work on my games shop menu where you can “buy” weapons and then use them. By buying weapons I mean accessing their fire weapon script and setting a small bool called IsActive to true which will then be used by the weapon switch script to determine which weapons it can switch and which weapon it can’t switch.
For now, it only works that way where if a weapon isn’t available it just disables the weapon. I don’t want that. I want to limit it so that way if the weapon isn’t available then the player can’t switch to it until he buys it.

So how do I do it from this code?

public class WeaponSwitching : MonoBehaviour {

    public int SelectedWeapon = 0;


    // Use this for initialization
    void Start () {
        SelectWeapon ();
       
    }
   
    // Update is called once per frame
    void Update () {

        int previousSelectedWeapon = SelectedWeapon;

        if (Input.GetKeyDown (KeyCode.Alpha1)) {
            SelectedWeapon = 0;
        }

        if (Input.GetKeyDown (KeyCode.Alpha2) && transform.childCount >= 1) {
            SelectedWeapon = 1;
        }

        if (Input.GetKeyDown (KeyCode.Alpha3) && transform.childCount >= 2) {
            SelectedWeapon = 2;
        }

        if (Input.GetKeyDown (KeyCode.Alpha4) && transform.childCount >= 3) {
            SelectedWeapon = 3;
        }


        if (previousSelectedWeapon != SelectedWeapon) {
                SelectWeapon ();
        }

     }

       
   



    void SelectWeapon(){

        int i = 0;
        foreach (Transform weapon in transform) {
            if (i == SelectedWeapon && weapon.GetComponent<FireWeapon>().IsActive == true) {
                    weapon.gameObject.SetActive (true);
                    weapon.GetComponent<FireWeapon> ().anim.CrossFadeInFixedTime ("Draw", 0.001f);
                } else {
                    weapon.gameObject.SetActive (false);
                }
                i++;
        }
    }
}

You are iterating through all of the weapons each time, regardless of their “IsActive” state. So if your new weapon has not been bought, you are still going into the SetActive(false) line for every weapon, which disables everything.

I’d probably use an entirely different structure with a list of all weapon GameObjects in this script, so I could then just say list[SelectedWeapon].Activate() and list[previousSelectedWeapon].Deactivate() instead of having this weird i variable you’re nesting inside the foreach loop.

If you want to keep this you need to check if a weapon has been set active before setting anything else inactive. As I said, I would do it differently, because now you need to iterate through the weapons two times, but this should work as well:

void SelectWeapon()
{
        int i = 0;
        bool hasChanged = false;
        foreach (Transform weapon in transform)
        {
            if (i == SelectedWeapon && weapon.GetComponent<FireWeapon>().IsActive == true)
            {
                    hasChanged = true;
                    weapon.gameObject.SetActive (true);
                    weapon.GetComponent<FireWeapon> ().anim.CrossFadeInFixedTime ("Draw", 0.001f);
            }
            i++;
        }
               
        i = 0;

        foreach (Transform weapon in transform)
        {
            if (i != SelectedWeapon &&  hasChanged = true)
            {
                    weapon.gameObject.SetActive (false);
            }
            i++;
        }
}