I’m trying to figure out the best way to wait for an animation to complete before showing a UI text panel.
I’ve created a chest object. When the user clicks on it, I’d like it to play an open animation and then display a message describing what is inside. I’ve tried doing this with coroutines, but Unity seems to be running the UI code in parallel with the character animation instead of waiting for it to complete.
I’m using coroutines since most of the animation example code I’ve come across uses coroutines. However, if there is a way to do it without coroutines, I’m open to that too. I’m just trying to find a robust solution that works.
public class ChestNPC : MonoBehaviour
{
public bool open = false;
public string itemId;
public int quantity = 1;
public bool isGold = true;
public override IEnumerator ActivateByPlayer()
{
if (open)
{
yield break;
}
open = true;
yield return StartCoroutine(doOpenAction());
yield return StartCoroutine(doMessage());
}
IEnumerator doOpenAction()
{
Animator anim = GetComponent<Animator>();
anim.SetBool("Open", open);
yield return new WaitForEndOfFrame();
}
IEnumerator doMessage()
{
if (isGold)
{
GameController.Instance.gold += quantity;
FindObjectOfType<ChatController>().Show(new string[]{"You found " + quantity + " gold"});
}
else
{
GameController.Instance.inventory.AddItem(itemId, quantity);
FindObjectOfType<ChatController>().Show(new string[]{"You found a " + itemId});
}
yield return new WaitForEndOfFrame();
}
}