I have a UI animation for a black screen that fades in (goes from transparent to black) and another that fades out (goes from black to transparent). I'm trying to play the fade-in animation (screen becomes black) and then execute some lines of code while the screen is still black and then play the fade-out screen.
I tried to achieve this using StartCoroutine(Text());
like so:
transitionAnim.Play("fade_in");
StartCoroutine(Text());
character.SetActive(false);
Damagedcharacter.SetActive(true);
transitionAnim.Play("fadeout");
IEnumerator Text() // <- its a standalone method
{
yield return new WaitForSeconds(1f);
}
I want these lines:
character.SetActive(false);
Damagedcharacter.SetActive(true);
to be executed during the black screen. but they are not. what happens is that they are executed first then the black screen fade in and out.
How can I make the black screen stays for a while and execute some lines during the black screen and then fade out?
CodePudding user response:
A Coroutine doesn't magically delay the code in the method it is started from.
Either move your lines to the end of the IEnumerator
like
transitionAnim.Play("fade_in");
StartCoroutine(Text());
IEnumerator Text()
{
yield return new WaitForSeconds(1f);
character.SetActive(false);
Damagedcharacter.SetActive(true);
transitionAnim.Play("fadeout");
}
Though personally I wouldn't mix animation and coroutine. Rather use Animation Events and have a callback for when you are faded to black and from there do stuff and then trigger the fade back.