- Home /
Coroutine fires once after destroying gameobjects in a scene and recreating new objects
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!
Answer by logicandchaos · Jan 09, 2020 at 01:19 PM
So does it work when it's reinstantiated or when it's not? if it works when it is reinstantiated you could just destroy it and reinstantiate it. It's not the most efficient solution but shouldn't cause any issues. If you doesn't work when reinstantiated then there has to be something missing..
Well the Enemy and Neutral game pieces get re instantiated between levels, and they have a very similar coroutine, but they work just find when transferring levels. Its my player object, when it gets destroyed and re instantiated it has this issue.
Whats even weirder is that if I make contact with another board piece during my initial turn after recreating the level from the main menu, the coroutine fixes itself and works as expected.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Cannot destroy Component while GameObject is being activated or deactivated 2 Answers
How to dynamically construct a reference to a game object by name 2 Answers
Mouse Player Movement Controller 1 Answer
There is no GameObject attached to this GameObject 2 Answers