# For loop comparing two different objects appears to not be running

So I’m working on a match 3-esque game and I’m currently trying to get my objects to sense whether or not they are adjacent to another object of their color. And by adjacent I mean adjacent from every angle; diagonally, horizontally and vertically. I’ve currently stored the exact coordinates of each object in an array from a script that spawns all the objects to the level. The array’s name is groupOfEnemies. On the object’s script, I have created a method called MatchCheck() and I call it whenever I click on an object with OnMouseDown(). I’ve pasted the MatchCheck() code further below.

Essentially, due to the way I’ve set up my game’s coordinates, I want each for loop to iterate down every possibly adjacent object, from left to right. To better explain, think of the numbers on a num pad as the locations of the objects in my next sentence: I want each object’s color to be compared to the object at number 5, starting with the object at the number 7 and working my way down to 1. Then I want to start back on top at 8 down to 2, until I reach the final object at number 3. You’ll notice that I set each for loops variable to check and iterate by 2. This has to do with how far apart my objects are from each other. So for example, since the object in the top left will, in relation to the center object, be -2 X and +2 Y coordinates away, I’m trying to tell the for loops to account for this mathematical consistency in relation to the current object doing the checking. The first if statement then makes sure the central object will not be checked for the same color, and then the second if statement checks if the object at the current coordinates has the same color as the central object (the body variable contains a string designating the object’s color).

``````public void MatchCheck()
{
int transX = (int)this.transform.position.x;
int transY = (int)this.transform.position.y;

for (int x = transX - 2; x > transX + 2; x += 2)
{
for(int y = transY + 2; y < transY - 2; y -= 2)
{
if(enemyArray.groupOfEnemies[x,y].transform.position != this.transform.position)
{
if(enemyArray.groupOfEnemies[x,y].GetComponent<BasicEnemy>().body == this.body)
{
match = true;
}
}
}
}
print(match);
}
``````

My trouble is that it appears the for loop isn’t running. I’ve tested that the method runs when called, but even when I put a Debug.Log after the first for loop, I get nothing. The only thing I can think of right now that might be causing the issue is my adding and subtracting from the for loop conditions, but I don’t understand why. Is that’s what causing the issue, and if so is there a better way around it? Or is there another problem here that I’m missing? If you think you need to see more code, please let me know.

it should read

`````` for (int x = transX - 2; x < transX + 2; x += 2)
``````

your had a greater-than instead of a less-than