Advanced C# problem: Timings incorrect in script, assigning a variable that is not updated yet in an array.
Dear developers,
I am trying to make a match-three game from scratch ( a game like Candy Crush ) and I stumbled upon a timing issue I can't seem to resolve. I do not know how to get started.
When I switch two pieces, at the end at their moving co-routine I set the pieces to their new position, and update the 2 dimensional array where I store all pieces and raise an event saying it has stopped moving. In a handeler I wrote, check if its partner piece has matches. (the one we swapped with) and check if the piece itself has matches.
If they both don't, obviously they swap back. This works.
But if one of the pieces has a match, clear out the matches. As the other has to stay in the same place. This doesn't work.
I check with the debugging tool and found the problem, but I don't know a way to solve this and reach out to you guys for help since I am yet a beginner.
Here is the code I use: PasteBin link
Thanks in advance, I hope you guys can help. Otherwise I will just start over.
The problem is this par example:
I have a blue piece at 0,0 [X,Y] coördinates. I have a yellow piece at 0,1 [X,Y] coördinates.
If I swap those 2 pieces, the blue one does his check in the handeler first. The new position is already updated, so at 0,1 we have the blue piece.
But when I calculate the partner piece, using it's previous position hence the 0,0 coordinate where the yellow one is supposed to be, it ALSO thinks it has a blue piece. It seems like the yellow piece's moving routine still has not finished and hence why it thinks there are 2 the same pieces.
Your answer
![](https://koobas.hobune.stream/wayback/20220612130054im_/https://answers.unity.com/themes/thub/images/avi.jpg)