Animation doesn't play while coroutine is running
When my scene starts, I start a coroutine that generates a puzzle which is displayed to the user. Sometimes, this generation can take a few seconds but usually quite fast. Because the user can also refresh the puzzle from the scene, I don't want to jump back and forth to a separate loading scene and instead just show a simple animation while the coroutine is running. I've tested two animations on the same sprite: an Animator of the sprite and a simple rotation on update. However, like the title says both of these aren't running while the coroutine is running. I can see that that animation works because after the coroutine finishes, both animations begin (see that SetActive(false) commented out below). How can I get this animation to work?
The structure is like this:
Game
Canvas
LoadingScreen [Game Object] (with LoadingAnimation script)
animation [Game Object] (with Sprite Renderer and Animator components)
PuzzleControl [Game Object] (with PuzzleControl script)
Here's the code:
public class PuzzleControl : MonoBehaviour
{
private PuzzleBuilder builder;
void Start()
{
builder = this.gameObject.AddComponent<PuzzleBuilder>();
LoadingScreen.SetActive(true);
StartCoroutine(builder.Refresh());
}
public void RebuildPuzzle()
{
LoadingScreen.SetActive(true);
StartCoroutine(builder.Refresh());
}
public void BuildComplete()
{
// This will be called by our builder -- currently turned off
//LoadingScreen.SetActive(false);
}
}
public class LoadingAnimation : MonoBehaviour
{
public GameObject animation;
private float rotateSpeed = 200f;
// Update is called once per frame
void Update()
{
animation.GetComponent<Transform>().Rotate(new Vector3(0f,0f, rotateSpeed * Time.deltaTime));
}
}
Answer by nmill99 · Jun 11, 2020 at 06:49 AM
I asked the same question on stackoverflow and got the answer I needed: https://stackoverflow.com/questions/62304510/animation-doesnt-play-while-coroutine-is-running
The issue was a misunderstanding of how coroutines work. Coroutines doesn't mean asynchronous. "Coroutines only return control of the cpu when they yield, each frame unity will re-enter the coroutine where it left off which gives the illusion of the code running at the same time when really it's just being sliced into chunks using the yield keyword."
I fixed my issue by adding a yield wait in a loop that was taking a long time to exit. I had it in another loop inside this loop but that caused the whole thing to slow down considerably so I backed it into the parent loop.