Why won't identical 'for' loops work?

Hi all,
So I’m making a game in which a player has to move a gem into a board to create matches, similar to Bejeweled. Now, I’m able to detect matching gems above and below using a for loop, but when I do the same for gems to the side, the function just doesn’t output anything. Could anyone help me figure out why this is the case?

Notes for the code: The ID of the gems increase from 1-56 from left-right, and IDCheck is the ID of the gem to the sides of which we’ll be checking for matches. The board is 8 rows of 7 gems, and finals is the array to hold the IDs of matching gems. Only the loop using var b (gems to the right) won’t work.

for (var a = IDCheck+7; a <= 56; a += 7) {
  var next1 = GameObject.Find(a.ToString());
  if (next1.gameObject.GetComponent.<SpriteRenderer>().sprite == object.gameObject.GetComponent.<SpriteRenderer>().sprite) {
    finals.Push(a.ToString());
  }
  else {
    break; }
}

for (var b = IDCheck+1; b <= 56; b ++) {
  var next2 = GameObject.Find(b.ToString());
  if (next2.gameObject.GetComponent.<SpriteRenderer>().sprite == object.gameObject.GetComponent.<SpriteRenderer>().sprite) {
    finals.Push(b.ToString());
  }
  else {
    break; }
  }

for (var c = IDCheck-7; c >= 0; c -= 7) {
  var next3 = GameObject.Find(c.ToString());
  if (next3.gameObject.GetComponent.<SpriteRenderer>().sprite == object.gameObject.GetComponent.<SpriteRenderer>().sprite) {
    finals.Push(c.ToString());
  }
  else {
    break; }
}

I agree that this is definitely not the most efficient or effective way to compare gems, personally I would create a Gem class, give it a gemtype variable for comparison and since you’re comparing in straight lines I would just use colliders and raycasts.

That, however, is not an answer to your question. You’re methodology with the a and c ‘for’ loops is sound, within the scope of your approach, the b ‘for’ loop is set up exactly the same, except it checks the gems to the right and then just continues through all the gems below, as others have pointed out, so if anything it could find too many identical gems. The problem must therefore be elsewhere, but as for fixing the b ‘for’ loop, I suggest the following:

for (var b = IDCheck+1; b % 7 != 1; b ++)

It checks gems to the right until it reaches the first gem in the next row, something similar would work for left side checking:

for (var d = IDCheck-1; d % 7 != 0; d --)

The side checking is wrong. The right side search is unbound and will iterate to the end of the array (which is a bit weird since you say it does not return anything … maybe you should test on more scenarios, or truly the bug is somewhere else), the left side search is missing. Try this:

int leftBound = 7 * (IDCheck / 7) + 1; // note that the division is on integers
int rightBound = leftBound + 6;

for (var b = IDCheck+1; b <= rightBound; b ++) {
   // ...
}

for (var d = IDCheck-1; d >= leftBound; d ++) {
   // ...
}

Also as others said, consider optimizing the code before proceeding to other scripts. It’s quite costly.