Grid Path finding not working and I can't find why

I found the solution. I’ll try to explain if anybody got the same problem. I had to change the function that detects neighbours of the analyzed square.
I had to make it so it prefer to move forward than in diagonals if the two tiles has the same cost.