Activating Bool Array in sequence.

What i want:

To be able to activate a the next / previous bool in the array in order from 0 to 4 / 4 to 0

    public bool[] selectNext;

    void Start()
    {
        selectNext = new bool[4];
        
        
    }
    private void Update()

// here i want my input for scrolling through the bools
    {
        if (Input.GetKeyDown(KeyCode.D)) { "Next bool = true previous bool = false }
    }

 if (Input.GetKeyDown(KeyCode.A)) { "Previous = true Next bool = false }
    }

If anyone could help achieve this or explain how to achieve this i would forever be in your debt.

Chris

You can use Array.IndexOf(). It will return the index of the first occurence of the element you’re looking for. In your circumstances, that first occurence will be the only occurence so this function will serve your needs.

if (Input.GetKeyDown (KeyCode.D)) { 
	int index = System.Array.IndexOf(selectNext, true);
	if (index < selectNext.Length - 1 && index != -1) {
		selectNext [index] = false; // Make current element false
		selectNext [index + 1] = true; // Make next element true
	}
}

if (Input.GetKeyDown (KeyCode.A)) { 
	int index = System.Array.IndexOf(selectNext, true);
	if (index > 0) {
		selectNext [index] = false; // Make current element false
		selectNext [index - 1] = true; // Make previous element true
	}
}

When only one of them should be active at a time, you really shouldn’t use an array for this. This just adds redundancy and unnecessary complexity. Just use a single int value and you get the same functionality.

int selected = 0;
int count = 4;

void Update()
{
    if (selected < count-1 && Input.GetKeyDown (KeyCode.D))
    {
        selected++;
    }
    if (selected > 0 && Input.GetKeyDown (KeyCode.A))
    {
        selected--;
    }
}

To see if selected is a certain value you can directly replace your old conditions like:

if (selectNext[1])

with

if (selected == 1)

If instead of clamping the min and max to 0 and count-1 you want it to wrap around, you can simply do:

    if (Input.GetKeyDown (KeyCode.D))
    {
        selected++;
        selected = (count+selected)%count;
    }
    if (Input.GetKeyDown (KeyCode.A))
    {
        selected--;
        selected = (count+selected)%count;
    }

This will make it wrap around automatically. So when you are at selected 0 and press A it will wrap around to “count-1” so 3 in this case. Likewise when at the top end when selected is 3 and you add one you get back down to 0. So the first variant will just stay at the min / max value (which is 0 / 3 for 4 elements) and the second one just wraps around.