- Home /
More loop problems
private bool[] loopBool = 3;
Script 1 public void ActivateScript1(){
int i = 0;
if(!loopBool[i]){
Script2.ActivateScript2();
}
for (; i < max; i++)
{
loopBool[i] = true;
}
}
Script 2
public void ActivateScript2(){
foreach (var item in collection)
{
Debug.Log("A");
}
}
ActivateScript1 is activated via a button. I want it so that if I press the button the debug message appears. Then I want that slot in the boolArray to turn true. And it should do this max 3 times since that is the size of the bool array.
Unfortunately as is now the first slot turn true and nothing happens after I press the button again. I know this might seem complicated ask for info if you are confused.
Answer by GeroNL · Feb 23, 2021 at 11:46 AM
hello, what do you mean about "nothing happens after I press the button again"?
Is the problem doesn't appear again? if it is because after slot [0] (cause 'i' is zero ) going to be true after that that it's never back to false, you must set it false to make it work again.
or the problem is doesn't make slot [1] and [2] true? if it's that you mean, that because you are never set "i" to slot that you want to be changed.
Hope it help.
Exactly. The first thing is what happens. I know that. I don't know how to solve it. If [0] = true then I want it to go the next [1]. It only stops when all the slots in the bool array is true.
Now it just checks if [i] is true which it is once [0] = true.
if you want to all true then all false, you can do like this:
// set it after iteration (for)
if(i == loopBool.length-1)
{
i = 0;
while(loopBool[loopBool.length-1])
{
loopBool[i] = false;
i++;
}
i = 0;
}
No, I want it so that when I press the button [0] = true. When I press the button again then [1] = true and so on.
Your answer
Follow this Question
Related Questions
How do I create a variable for each GameObject that I added to an array by using a loop? 2 Answers
foreach loop stops at first item 1 Answer
Not able to make a foreach loop 2 Answers
Array Element in Foreach loop 1 Answer
How to have a loop run itself again if returned value is a known exception, with a new return value. 1 Answer