- Home /
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.
Answer by abstractfragment · Jun 19, 2018 at 05:47 PM
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;
}
}
}
}
Answer by top2323 · Jun 19, 2018 at 04:08 AM
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.
Did a variation of this, thanks to your help though I looked at counters and found a similar solution.
Your answer
Follow this Question
Related Questions
Problem with arrays and while loops 0 Answers
Argument if out of range: loops and arrays 2 Answers
create an array of unknown size in a loop 1 Answer
Add existing variables to an array? 1 Answer
loop with array system 0 Answers