Trying to get the number of buttons being stood on in a scene.

Want a door to constantly check whether all the buttons in a scene are being held down. I think I might be going about this all wrong. This is done in Update()

foreach (GameObject button in buttons){
			buttonScriptARR thisButton = button.GetComponent<buttonScriptARR> ();
			if (thisButton.isButtonDown == true) {
				for (int i = 0; i < buttons.Length; i++) {
					numberOfButtonsDown++;
			
				}
			}
			else if (thisButton.isButtonDown == false){
				for (int i = 0; i < buttons.Length; i++)
					numberOfButtonsDown--;

					}
		}
	}

I don’t know a good way to end the loop, what happens now is standing on the button causes the numberOfButtonsDown to increase toward infinity and vice versa for not standing on it.

What you might do is have an int variable in your button script, 1 if held, 0 if not, and then in your foreach you could go through every button script, grab it, and add it into a central counter.

Solved this myself, didn’t need the foreach loop and for loops. Also added script to reset the number of buttons held down to zero at the beginning of each fixed update.

void Update () {
		numberOfButtonsDown = 0;
		Debug.Log ("Rest take");
		
		foreach (GameObject button in buttons){
			buttonScriptARR thisButton = button.GetComponent<buttonScriptARR> ();
			if (thisButton.isButtonDown == true) {
				{
					
					numberOfButtonsDown++;
					Debug.Log ("down take" + numberOfButtonsDown);
			
				}
			}
			else if (thisButton.isButtonDown == false){
					
					
				Debug.Log ("up take" + numberOfButtonsDown);

					}
			if (numberOfButtonsDown < 0) {
				numberOfButtonsDown = 0;
			}
		}
	}
}