- Home /
How to implement a "wait" after an animation has finished?
I am trying to add some public functionality to my animation script so that it can be adjusted from the inspector. The intended functionality that I am looking for is when "AddSequenceDelay" is checked (bool=true) that I can then check for the last frame of the animation and set a "Delay Duration".
The problem I seem to be experiencing is that the the WaitForSeconds is not being processed even though I have determined through Debug.log usage that the function is being accessed.
IEnumerator PlayLoop(float delay)
{
//Wait for the time defined at the delay parameter
yield return new WaitForSeconds(delay);
//Advance one frame
frameCounter = (++frameCounter)%textures.Length;
//Stop
StopCoroutine ("PlayLoop");
if (AddSequenceDelay == true && frameCounter <= 0)
{
Debug.Log ("TEST");
StartCoroutine("SequenceDelay");
}
//Stop this coroutine
StopCoroutine ("PlayLoop");
IEnumerator SequenceDelay()
{
yield return new WaitForSeconds(DelayDuration);
StopCoroutine ("SequenceDelay");
}
I appreciate any help that can be provided on this debacle. :)
Answer by Autonomous · Jul 13, 2013 at 05:14 AM
It turns out that my ordering was just a bit off. To resolve the problem I just reordered the wait code to be checked prior to the playing the loop. Only issue is it waits for the full delay to be processed before the animation plays for the first time. (Example: Delay of 5 seconds will cause the animation to be held up for 5 seconds before it plays for the first time)
IEnumerator PlayLoop(float delay) {
if (AddSequenceDelay == true && frameCounter <= 0)
{
yield return new WaitForSeconds(DelayDuration);
}
//Wait for the time defined at the delay parameter
yield return new WaitForSeconds(delay);
//Advance one frame
frameCounter = (++frameCounter)%textures.Length;
//Stop this coroutine
StopCoroutine ("PlayLoop");
}
I resolved the issue with the animation being delayed on the first cylce by adding a simple bool check before the sequence delay if statement.
if (firstCycle == true) {
// Now that the first cycle has taken place, this code will no longer need to be ran
firstCycle = false;
//Wait for the time defined at the delay parameter
yield return new WaitForSeconds(delay);
//Advance one frame
frameCounter = (++frameCounter)%textures.Length;
}
Answer by JohnnySunshine · Jul 12, 2013 at 10:04 AM
Try
yield return StartCoroutine("SequenceDelay");
Also, you don't have to call StopCoroutine at the end of a function, it will stop when it reaches the end.
Johnny there was no change. The problem isn't that the coroutine isn't being called, it's that the WaitForSeconds is not being applied when the coroutine is run.
Your answer
Follow this Question
Related Questions
WaitForSeconds bug,Coroutine Bug 1 Answer
(Rapid fire) How to accurately wait for a very short amount of time? 4 Answers
function behaves unexpectedly inside a coroutine. 2 Answers
Gun script help 1 Answer