Reputation: 27
My grid is on an array and I iterate through each grid tile to check to see if it is null and if the one above it isn't it will drop the block to the one below.
The code worked perfectly fine when I did this instantly, but once I added a coroutine so I drop the blocks slowly it stopped working. I'm fairly sure it's because the loop is checking blocks before they are set properly but I'm not sure how to go about fixing that problem.
private void UpdateBoard()
{
// Need to figure out how to adjust my grid objects when a word has been destroyed.
for (int x = 0; x < grid.width; x++)
{
for (int y = 0; y < grid.height - 1; y++)
{
if (grid.tiles[x, y] == null && grid.tiles[x, y + 1] != null)
{
StartCoroutine(BlockFall( x, y + 1 ));
// grid.tiles[x, y + 1].transform.position = new Vector2(grid.tiles[x, y + 1].transform.position.x, grid.tiles[x, y + 1].transform.position.y - 1);
}
}
}
}
public IEnumerator BlockFall(int posX, int posY)
{
float startY = 1;
grid.tiles[posX, posY].pos.y = grid.tiles[posX, posY].pos.y - 1;
grid.tiles[posX, posY - 1] = grid.tiles[posX, posY];
while(startY > 0)
{
startY -= 0.25f;
grid.tiles[posX, posY].transform.position = new Vector2(grid.tiles[posX, posY].transform.position.x, grid.tiles[posX, posY].transform.position.y - 0.25f);
yield return new WaitForSeconds(0.1f);
}
grid.tiles[posX, posY] = null;
}
Those are the two functions that are important. It's a bit messy right now perhaps, but it worked.
What happens now, is that the first block will fall, but the ones above it won't. That was working when instant though.
Upvotes: 0
Views: 416
Reputation: 27
All right, I fixed the issue and I guess I didn't give enough information to solve the problem the way I ended up doing it but I'll add what I did.
I have an activeLetter variable which contains the current block's information. I moved my UpdateBoard() into a FixedUpdate function and then set a check to make sure it ignored the activeLetter position.
It works perfect now.
Upvotes: 0
Reputation: 2862
Try running the whole loop in the Coroutine or using "yield return new WaitForEndOfFrame();" at the beginning of the Coroutine. Maybe this way you will find whats the issue.
Coroutines are not sync with unity update functions in case you have code in Update(), FixedUpdates()... and you may face problems because of that.
Upvotes: 1