Loop through a list, meet condition before passing to next element
Hey i have a hexagon grid that spawns in 10x10.
These tiles are all empty at first but with a for-loop i go through each tile and generate a random color and tag. This loop goes through a tile and then another each frame:
for (int i = 0; i < tileList.Count; i++)
{
tileList[i].GetComponent<Tile>().RandomColor();
yield return new WaitForFixedUpdate();
}
This way it starts at element [0] in the list and generates a random color and then goes to element [1] and etc.
However i want the current element to meet a condition before it can pass over to the next element in the loop. If it doesnt meet a condition it will go through another loop and when the condition is true that loop will end the original loop will continue to the next element.
Is this possible? The reason for this is i want to be able to generate a grid with colored tiles that arent neighbour with eachother of the same color. So if i generate a color for a tile in the loop but next to it is a tile with the same color, i want the loop to continue until it gets to a color that a neighbour doesnt have and then it can pass over to the next tile.
Hope i made myself clear, ask if it seems confusing and ill try to be more simple.
Thanks
EDIT: This is how RandomColor() looks like in Tile.cs, the script that all tiles are holding:
public Material[] color;
public static string[] tags = {"blue", "green",
"purple", "red", "yellow"};
public void RandomColor()
{
int index = Random.Range(0, color.Length);
GetComponent<Renderer>().material = color[index];
gameObject.tag = tags[index];
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612080844im_/https://answers.unity.com/themes/thub/images/avi.jpg)