I’m making a task that runs within a coroutine loop. If the player runs out of time, I stop the coroutine, reset some variables (but keep others such as score) and start the coroutine again. After this time-out happens a couple of times the player object stops working. It has it’s own controller for player object movement and properties, but when I press the arrow keys it doesn’t move.
Is it bad to potentially start and stop the main coroutine so often? I don’t want to reload the entire level because I need to keep track of how many times they’ve gone through the main coroutine, their score, what game objects from the pool have been used, etc. Would it be better to store everything in PlayerPrefs and reload the entire scene? Is it just the method I’m using to start and stop the coroutines?
My method so far:
In the game controller, I have a timer running in Update. When the timer runs out it stops the main coroutine and calls the TimeOut function I made (I removed some of the details of what I’m resetting to save space):
public void TimeOut ()
{
timeOutText.text = "Time's up!";
/* Play time out beep */
/* Reset all currently active bugs to inactive */
/* Reset example bug and it's properties */
/* Reset feedback & timer properties */
blocks +=1;
StartCoroutine(BlockOver()); // I read that this method is better for calling coroutines, so I use this when I don't need to stop them
}
But that can’t restart the coroutine because it’s not an IEnumerator, so I then have it call an IEnumerator BlockOver() to start the coroutine again:
IEnumerator BlockOver ()
{
/* Wait briefly between blocks before making next example bug */
yield return halfBlockWFS; // I created variables for WaitForSeconds at the start and reuse them as needed
/* Start main block coroutine over again */
StartCoroutine ("SpawnWaves"); // I use the string to start the coroutine because this is the one I keep starting/stopping and I think you need the string version to use StopCoroutine
}
Any help would be appreciated. I’ve been reading a lot of Unity and C# forums/documentation, but couldn’t find anything specific to the consequences of starting/stopping game controller coroutines frequently on other game objects.