Coroutine은 yield구문을 만다면 곧바로 실행이 됩니다.
스크립트를 중지하거나 제거하면 yield이후의 구문은 실행되지 않습니다.
아래는 잘못된 코드입니다.
void Update()
{
if(health < 0)
{
StartCoroutine(Die());
Destroy(gameObject); //or enabled = false;
}
}
IEnumerator Die()
{
animation.Play("wobble");
yield return new WaitForSeconds(3);
//This will never be called
animation.Play("die");
}
아래와 같이 수정해야 정상동작을 수행합니다.
bool dying;
void Update()
{
if(dying) return;
if(health < 0)
{
StartCoroutine(Die());
}
}
IEnumerator Die()
{
dying = true;
animation.Play("wobble");
yield return new WaitForSeconds(3);
animation.Play("die");
yield return new WaitForSeconds(3);
Destroy(gameObject);
}
댓글 없음:
댓글 쓰기