I’m having this very frustrating issue while using Coroutines to move my board pieces across my board.
public void MovePlayer()
{
StartCoroutine("Move");
}
private IEnumerator Move()
{
for (int i = 0; i < this.GetMovement(); i++)
{
//game-specific logic here
yield return new WaitForSeconds(.1f);
}
// allows the next board piece to move.
FinishMove();
}
The call to the Move Coroutine is within the gameobject in which the script is attached to (ie the Player script contains both MovePlayer() and Move()
This coroutine works great the first time I enter the game, all the board pieces move perfectly.
When I transfer levels, I destroy the level completely and regenerate the board and enemies (not player) and the movement coroutine works perfectly still (this involves going to a new scene before going back to the level scene).
It gets really weird when I try to do the same thing but going to the main menu and back to the level scene (not loading any saves just starting a fresh game). When I do this the coroutine fires only 1 time when before it would fire for each movement point my player or enemies have remaining.
The only difference between going between levels and going to the main menu and starting a new game is that the Player game object gets destroyed and recreated. The level scene is (and enemies) are all destroyed the same way as advancing to the next level.
I’ve debugged the Player game object and it’s being instantiated with all the correct values and I’m honestly lost to where to look next.
I’ve quadruple checked the loop condition is being satisfied for additional loops, the yield return new WaitForSeconds just never fires more than once in this case.
If anyone knows how this could be happening I would greatly appreciate it!