How to refactoring this big if condition? weapon is a list and it;s gameobject
How to refactoring this big if condition? weapon is a list and it;s gameobject
public class Customanger : MonoBehaviour
{
public List<GameObject> weapon;
public static Customanger singleton;
private void Awake() => singleton = this;
};
if (Customanger.singleton.weapon[1].activeSelf ||
Customanger.singleton.weapon[2].activeSelf ||
Customanger.singleton.weapon[3].activeSelf ||
Customanger.singleton.weapon[4].activeSelf ||
Customanger.singleton.weapon[5].activeSelf ||
Customanger.singleton.weapon[8].activeSelf ||
Customanger.singleton.weapon[10].activeSelf ||
Customanger.singleton.weapon[12].activeSelf ||
Customanger.singleton.weapon[13].activeSelf ||
Customanger.singleton.weapon[14].activeSelf ||
Customanger.singleton.weapon[15].activeSelf ||
Customanger.singleton.weapon[16].activeSelf ||
Customanger.singleton.weapon[17].activeSelf)
{
dosomething();
}
You can try .Any()
,
Determines whether any element of a sequence exists or satisfies a condition.
//Here I considered length of Weapon array/list is 17
if (Customanger.singleton.weapon.Any(x => x.activeSelf))
{
dosomething();
}
If you would like to check same condition for subset of weapons, then you can try below,
var subSetOfWeapons = Customanger.singleton.weapon.GetRange(1, 17);
if (subSetOfWeapons.Any(x => x.activeSelf))
{
dosomething();
}
For more details: List.GetRange(int index, int count)