您如何进行脚本循环,直到找到具有特定布尔值的孩子,然后从当前跳到具有特定布尔值的孩子? (C#)

我正在尝试创建一个库存系统,其中“如果显示枪在插槽中,那么玩家就可以使用真正的枪”。在我的武器切换脚本中,我想这样做,因此,如果我尝试切换武器,它将跳至显示插槽中的下一个武器。 (这令人困惑,我知道,我只是一个初学者,正在尝试进行测试)。我尝试执行此操作的方法是检测下一个真正的枪是否启用了bool。我希望脚本能通读其所有子级(玩家可以选择的所有可能的枪支),并查看启用/禁用了bool的枪支。如果玩家想切换武器,我想跳过所有禁用bool的武器,直接跳到下一个启用bool的武器。任何帮助将不胜感激! C#

这是武器切换脚本:

 using UnityEngine;

 public class WeaponSwitching : MonoBehaviour
 {
 public int selectedWeapon = 0;
 public Transform nextGun;
 public Transform currentGun;
 public Transform previousGun;
 public int gunValue;
 public int preGunValue;

 void Start()
 {
     SelectWeapon();
 }

 void Update()
 {
     int previousSelectedWeapon = selectedWeapon;


     /* I tried doing it here where it will detect if the next or previous weapon has the bool enabled, and if it does it will skip to the gun after that, but I eventually deemed it ineffective.
     gunValue = selectedWeapon + 1;
     preGunValue = selectedWeapon - 1;

     if (selectedWeapon >= transform.childCount - 1)
     {
         gunValue = 0;
     }

     if (preGunValue == -1)
     {
         preGunValue = 3;
     }

     nextGun = transform.GetChild(gunValue);
     currentGun = transform.GetChild(selectedWeapon);
     previousGun = transform.GetChild(preGunValue);
     // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/


     if (Input.GetAxis("Mouse ScrollWheel") < 0f)
     {
         if (Weapon.isReloading == false)
         {
             if (PauseMenu.GameIsPaused == false)
             {
                 if (selectedWeapon >= transform.childCount - 1)
                 {
                     selectedWeapon = 0;
                 }
                 else
                 {
                     selectedWeapon++;
                 }
             }
         }
     }

     if (Input.GetAxis("Mouse ScrollWheel") > 0f)
     {
         if (Weapon.isReloading == false)
         {
             if (PauseMenu.GameIsPaused == false)
             {
                 if (selectedWeapon <= 0)
                 {
                     selectedWeapon = transform.childCount - 1;
                 }
                 else
                 {
                     selectedWeapon--;
                 }
             }
         }
     }

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

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

}