Help with Arrays - Check if only 1 is toggled

Hey guys,

I don’t know if I’m going about this the right way. Basically I need to be able to click a toggle, and when I click the toggle, I need to change the alpha of a few buttons. I wanted to do it in an Array because I’ll be adding more buttons called Slots later

public void ShowAllSlot()
    {
        
        GameObject[] buildOptionsFinder = GameObject.FindGameObjectsWithTag("BuildOptions");

        buildOptions = new Toggle[buildOptionsFinder.Length];

        for (int i = 0; i < buildOptionsFinder.Length; i++)
        {
            buildOptions _= buildOptionsFinder*.GetComponent<Toggle>();*_

//Debug.LogError(buildOptions[1]);
if (buildOptions*.isOn )*
{
GameObject[] slotFinder = new GameObject[transform.childCount];

for (int ii = 0; ii < transform.childCount; ii++)
{
slotFinder[ii] = GameObject.Find(“Level1Slots”).transform.GetChild(ii).gameObject;

Image button = slotFinder[ii].transform.GetChild(0).transform.GetChild(0).transform.GetComponent();

Debug.LogError(“IT SHOULD BE SHOWING NOW”);
button.color = Alpha40;

}
}

}

}
I can’t seem to figure a way to say :
if (1 out of all buildOptions*.isOn == true)*
do this
else if (all buildOtions*.isOn == false)*
do this
Thanks!

You could use a bool and a foreach loop to check that at least one of the toggles is selected. Something like this (I haven’t tested this code sorry, just writing it from memory).

bool isToggled = false; //declared at the start of your class

foreach(Toggle t in buildOptions){
	if(t.isOn){
		isToggled = true;
	}
}

if(isToggled){
	//Do something
}else{
	//Do something else
}

isToggled = false;

I may be misunderstanding your question, but in the event that I’m not, here is an example that uses Linq to show another way to tackle your problem. Your question read to me as if you only wanted to do something if only one true element was in an array and to do something else otherwise. The above for loop runs the risk of changing the toggled flag back to false if the last element happens to be false.

In your case, the test would be more like if(buildOptions.Any(x => x.isOn) … or if you’re just looking for the count to be one and only one, if(buildOptions.Count(x => x) == 1).

using System;
using System.Linq;

public static void Main()
{
		var n1 = new bool[3] { true, true, true };
        var n2 = new bool[3] { true, false, false };
        var n3 = new bool[3] { false, false, false };

        if (n1.Any(x => x)) // short for (x => x == true)
        {
            Console.WriteLine("N1 has at least one that is true");
        }
		
		if (n2.Any(x => x))
        {
            Console.WriteLine("N2 has at least one that is true");
        }
		
		if (n3.All(x => !x)) //short for (x => x != true)
        {
            Console.WriteLine("N3 has none that are true");
        }
		
		// Count
		var n1TrueCount = n1.Count(x => x);
		var n2TrueCount = n2.Count(x => x);
		var n3TrueCount = n3.Count(x => x);
		
		Console.WriteLine("N1 True Count: " + n1TrueCount);
		Console.WriteLine("N2 True Count: " + n2TrueCount);
		Console.WriteLine("N3 True Count: " + n3TrueCount);
		
		
		//Testing Count
		if(n1.Count(x => x) == 3)
		{
			Console.WriteLine("Do This");
		}
}