Jordi
Jordi

Reputation: 166

Stop one code in a for-loop in unityscript

I've got a for-loop which you can see at the bottom of this question, inside this for-loop are multiple if-statements, I want it that if one of those if-statement is called, that if-statement gets disabled for the rest of that for-loop. Is there anyway of doing that?

for (var i = 0; i < chunksInScene.length ; i++) {
    if(chunksInScene[i].name.Substring(0,5) == "Chunk" || chunksInScene[i].name.Substring(0,5) == "_TERR" || chunksInScene[i].name.Substring(0,5) == "ACTIV") {
        if(chunksInScene[i].tag != "Player") {
                if(chunksInScene[i].Equals(chunk1)){load1 = false;continue;}
                if(chunksInScene[i].Equals(chunk2)){load2 = false;continue;}
                if(chunksInScene[i].Equals(chunk3)){load3 = false;continue;}
                if(chunksInScene[i].Equals(chunk4)){load4 = false;continue;}
                if(chunksInScene[i].Equals(chunk5)){load5 = false;continue;}
                if(chunksInScene[i].Equals(chunk6)){load6 = false;continue;}
                if(chunksInScene[i].Equals(chunk7)){load7 = false;continue;}
                if(chunksInScene[i].Equals(chunk8)){load8 = false;continue;}
                if(chunksInScene[i].Equals(chunk9)){load9 = false;continue;}
                if(chunksInScene[i].Equals(chunk10)){load10 = false;continue;}
                if(chunksInScene[i].Equals(chunk11)){load11 = false;continue;}
                if(chunksInScene[i].Equals(chunk12)){load12 = false;continue;}
                if(chunksInScene[i].Equals(chunk13)){load13 = false;continue;}
                if(chunksInScene[i].Equals(chunk14)){load14 = false;continue;}
                if(chunksInScene[i].Equals(chunk15)){load15 = false;continue;}
                if(chunksInScene[i].Equals(chunk16)){load16 = false;continue;}
                if(chunksInScene[i].Equals(chunk17)){load17 = false;continue;}
                if(chunksInScene[i].Equals(chunk18)){load18 = false;continue;}
                if(chunksInScene[i].Equals(chunk19)){load19 = false;continue;}
                if(chunksInScene[i].Equals(chunk20)){load20 = false;continue;}
                if(chunksInScene[i].Equals(chunk21)){load21 = false;continue;}
                if(chunksInScene[i].Equals(chunk22)){load22 = false;continue;}
                if(chunksInScene[i].Equals(chunk23)){load23 = false;continue;}
                if(chunksInScene[i].Equals(chunk24)){load24 = false;continue;}
                if(chunksInScene[i].Equals(chunk25)){load25 = false;continue;}
        }
    }
}

Upvotes: 2

Views: 161

Answers (3)

Marconius
Marconius

Reputation: 713

As an alternative to the other answers, you could use delegates. Make each if statement a delegate, put them all in a Set, then just have the main loop going over the entire set. Once an if is triggered, remove it from the set and continue.

Upvotes: 0

nicael
nicael

Reputation: 18995

Add else and whitespace after the each if statement. (exept of the last if)

if(chunksInScene[i].Equals(chunk1){load1 = false;continue;}else
if(chunksInScene[i].Equals(chunk2){load2 = false;continue;}else
...

Upvotes: 1

axwcode
axwcode

Reputation: 7824

Since you don't want to stop the loop, but for the reminder of the loop ignore the if statement that executed. Then you need to create a condition for every if statement.

You can create an array of type boolean and set it to false once inside one of those ifs. Like this:

var myArray : boolean[] = new boolean[NUMBER_OF_IFs];

if(myArray[0] && chunksInScene[i].Equals(chunk1)
{
   load1 = false;
   myArray[0] = false;
   continue;
}

You need to make sure that for each element of the array there is an element that represents an if statement.


This is how it would look like in your code:

var myArray : boolean[] = new boolean[25];

for (var i = 0; i < chunksInScene.length ; i++) 
{
   // I changed this part, the long if statement was driving me crazy.
   var temp = chuckInScene[i].name.Substring(0,5);

   if((temp == "Chunk" || temp == "_TERR" || temp == "ACTIV") && chunksInScene[i].tag != "Player")
   {
       if(myArray[0] && chunksInScene[i].Equals(chunk1))
         {load1 = false; myArray[0] = false;continue;}

       if(myArray[1] && chunksInScene[i].Equals(chunk2))
         {load2 = false; myArray[1] = false; continue;}

       if(myArray[2] && chunksInScene[i].Equals(chunk3))
         {load3 = false; myArray[2] = false; continue;}

       .
       .
       .
       // You write the rest.
       if(myArray[25] && chunksInScene[i].Equals(chunk25))
         {load25 = false; myArray[25]; continue;}
    }
}

Upvotes: 3

Related Questions