- Home /
Even more loop problems
if(!loopBool[i]){
Script2.ActivateScript2();
}
I have a bool array called loopBool. I want to be able to loop through the array to check if each element in the array is true or not. If it's true then it calls ActivateScript2() which is located in a separate script.
The problem is that I have this function tied to a button. So I press the button. The function gets called no problem. I press it again. Nothing happens. Because [i] is now true ActivateScript2() does not get called. I tried putting it within a for loop like this:
for (int i = 0; i < max; i++)
{
if (!loopBool[i])
{
stats.Activate();
}
}
But that doesn't work either as ActivateScript2 gets called multiple times which is not what I want (see my previous question "Loop problems" for more) I only want it to run ONCE but I don't want it to only run so long as [i] != true. So I don't know what to do. Any ideas? Ask if you want clarification.
Answer by pauldarius98 · Feb 24, 2021 at 07:17 PM
Ok, so i readed the other post too and if i got it right, you want Script2.ActivateScript2() to be called when you press the button for the first 3 times, right? IE: I press the button, ActivateScript2 gets called, I press it the second time and it gets called again, i press it 3rd time and it gets called again and when i press it the 4th time, nothing should happen, right?
If that is the case, than instead of a bool array i would use a counter, like in the code below
int counter = 3; //
public void ActivateScript1()
{
if (counter <= 0)
{
return;
}
counter--;
Script2.ActivateScript2();
}
If I'm wrong then please provide more informations
Thank you for responding.
if(!loopBool[i]){
Script2.ActivateScript2();
}
This is the code that causes all the problems and it is very difficult for me to articulate why but basically I want the ActivateScript2() function to only activate if I: 1.press the button, 2.the bool in the array is presently not true 3. after those conditions are fulfilled the script activates and bool becomes true and 4. it will then "continue" to the next bool that is not true and do the same thing.
Once a bool is true the Activate() function should not work on that. Activate should just activate whenever the button is pushed and check which bools are true or false. The only way I know how to do this is by using a loop but then each bool gets "Activated" which I don't want. Am I making any sense? Because I'm finding it harder and harder to explain what I'm trying to do honestly.
Answer by Ermiq · Feb 25, 2021 at 02:40 AM
After reading your comment in the other answer, I think I understand what you want. Probably. On the button click the program should look into the loopBool
, find a fist value that is currently false
, switch it to true
, done, wait a for the next click. Right? If so then:
for (int i = 0; i < max; i++)
{
if (!loopBool[i])
{
stats.Activate();
loobBool[i] = !loopBool[i]; // set the value to opposite (was false, becomes !false, therefore true)
break; // stop the 'for()' loop, because we already found what we wanted
}
}
I'm afraid not. The problem remains. It's like this:
[False] [False] [False]
I press the button. It checks is loopBool[i] (<- "i" is the problem) false? Yes! Then:
[True] [False] [False]
I press the button again. It checks is loopBool[i] false? No! (Because the first one is true even though I want the second one to turn true so then nothing happens, that's the problem).
So, what is the max
? From the results you describe you have max = 1
, and the loop does check only the first element in the loopBool
. You need, for example, check all the elements in the loopBool
like this:
for (int i = 0; i < loopBool.Length; i++)
{
if (!loopBool[i])
{
stats.Activate();
loobBool[i] = !loopBool[i];
break;
}
}
Yep did that. Doesn't work. I'm kinda going crazy. I feel like I've tried everything at this point.
Your answer
Follow this Question
Related Questions
Making an animation stop once it finishes (trouble with booleans) 1 Answer
How to deactivate all GameObject in a array, except last one 4 Answers
Rearranging pre-existing objcets into a grid 2 Answers
Getting an error with my for/in loop! 3 Answers
How do couroutines handle If Statements within a for loop? 1 Answer