Help with weapon swapping

I’m trying to add weapon swapping in my game, it works but i want to only be able to swap weapons if the weapon has an enabled bool called “isCarrying” on it , this script goes through all of the weapons that are child objects of it and enables and disables them, but i only want them enabled if their own “Shooting” script has the “isCarrying” bool enabled, and if not it just skips over onto the next weapon that does. I hope you can understand what I want, all help is appreciated.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponSwap : MonoBehaviour
{
    public int selectedWeapon = 0;

    // Start is called before the first frame update
    void Start()
    {
        SelectWeapon();
    }

    // Update is called once per frame
    void Update()
    {
        int previousSelectedWeapon = selectedWeapon;

        //scroll up
        if (Input.GetAxis("Mouse ScrollWheel") > 0f)
        {

            if (selectedWeapon >= transform.childCount - 1)
                selectedWeapon = 0;
            else
                selectedWeapon++;
        }

        //scroll down
        if (Input.GetAxis("Mouse ScrollWheel") < 0f)
        {
            if (selectedWeapon <= 0)
                selectedWeapon = transform.childCount -1;
            else
                selectedWeapon--;
        }

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

    void SelectWeapon()
    {
        int i = 0;
        foreach (Transform weapon in transform)
        {
            if (i == selectedWeapon)
                weapon.gameObject.SetActive(true);
            else
                weapon.gameObject.SetActive(false);
            i++;
        }
    }
}

If you want to check the value of variables in another script you can use isWeaponBeingCarried = weapon.gameObject.GetComponent ().IsCarrying; // This sets isWeaponBeingCarried to the value of IsCarrying from the shooting script in the weapon you want to switch to
If(isWeaponBeingCarried == True)
//Switch to the weapon
If you don’t understand something or this is not what you want tell me

I don’t know where to implement that in the code

The new stuff are in lines 32 and 33 and lines 43 and 44
Make sure that the IsCarrying variable in shooting is a public bool

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
public int selectedWeapon = 0;

// Start is called before the first frame update
void Start()
{
SelectWeapon();
}

// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;

Debug.Log (selectedWeapon);
Debug.Log (transform.GetChild (selectedWeapon).name);

//scroll up
if (Input.GetAxis(“Mouse ScrollWheel”) > 0f)
{

if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
if(transform.GetChild(selectedWeapon).gameObject.GetComponent().IsCarrying == false)
selectedWeapon++;
}

//scroll down
if (Input.GetAxis(“Mouse ScrollWheel”) < 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount -1;
else
selectedWeapon–;
if(transform.GetChild(selectedWeapon).gameObject.GetComponent().IsCarrying == false)
selectedWeapon–;
}

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

void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
}
}
}

The code doesn’t work, I get a the error “Transform child is out of bounds”

Edit: Idk what happened but the code suddenly works