- Home /
For Loop Array, skip 1 value
Hi guys,
I got a question about an array in a for loop here is my code:
for (int i = 0; i < theScrews.Length; i++) {
theScrews [i].renderer.material.color = Color.black;
}
As you can see there is nothing wrong with it. But my question is, is it possible to skip for example theScrews[2]. So it wont change the color.
Here is the "psuedo code"
for (int i = 0; i < theScrews.Length; i++) {
theScrews [i].renderer.material.color = Color.black;
except for theScrews[2]
//theScrews[2] doesn't change to the color black
}
Answer by Lo0NuhtiK · Apr 03, 2014 at 08:24 AM
Make it skip the number 2.
for(int i = 0 ; i < whatever ; i++)
{
if(i == 2) continue ;
//do the other stuff here
}
Thanks, I was thinking way to hard for this :p
Will post new code in a few seconds
Answer by barker_s · Apr 03, 2014 at 08:29 AM
The easiest way would be to add an if statement, like this:
for (int i = 0; i < theScrews.Length; i++) {
if(i != 2) //
theScrews [i].renderer.material.color = Color.black;
}
You could also check against multiple values (for example, if you'd like to omit objects at index 2 and 5):
List<int> exceptions = new List<int>();
exceptions.Add(2);
exceptions.Add(5);
for (int i = 0; i < theScrews.Length; i++) {
if(!exceptions.Contains(i))
theScrews [i].renderer.material.color = Color.black;
}
Your answer
Follow this Question
Related Questions
GameObject array problems c# 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to assign variables to each GameObject in an array 1 Answer