- Home /
Wait for 1/4 seconds before executing the 'if' statement
if (mTrackableBehaviour.TrackableName == "Image1") { yield WaitForSeconds (0.25); pikachu.SetActive (true); StartSource.PlayOneShot (StartClip); } I want my program to wait for 1/4 seconds before executing the program written in the if statement. I thought, adding delay should work, I looked it up and found that the 'WaitForSeconds" ought to do it, but it didn't. I have used various other ways to implement this function like, 'yield return new WaitForSeconds', but didn't work either. All I want is to delay the if statement a bit.
Answer by Casiell · Oct 19, 2018 at 10:59 AM
Ok, what's the rest of the method? Because for yield return to work, method return type must be IEnumerator and you have to call it with StartCoroutine. Here is a link, so you can read some more about coroutines.
Your method should look something like this:
private void StartDelayedAnimation()
{
StartCoroutine(DelayedAnimation());
}
private IEnumerator DelayedAnimation()
{
if (mTrackableBehaviour.TrackableName == "Image1")
{
yield return new WaitForSeconds(0.25f);
pikachu.SetActive(true);
StartSource.PlayOneShot(StartClip);
}
}