- Home /
Switching between animations using if statements bug.
Hey guys, so I've been hitting my head against a wall for a few hours trying to fix this bug. So I have created an animation that goes for 12 seconds to only play once if it hasn't been played before (PlayerPrefs.SetInt ("Animation " + WorldCompleted + " Shown", 1)), then after that animation has finished playing I want it to change to a looping animation. However, when the one time animation finishes playing an all looks good, it then repeats the onetime animation when it should have switched to the looping animation.
void Start () {
MeshRendered = GetComponent<MeshRenderer> ();
WorldIsCompleted = PlayerPrefs.GetInt ("World " + WorldCompleted + " Completed");
if (PlayerPrefs.GetInt ("Animation " + WorldCompleted + " Shown") == 0 && WorldIsCompleted == 1)
{
print ("1");
PlayAnimation (0);
StartCoroutine( Wait ());
}
else if (WorldIsCompleted == 1)
{
print ("2");
StartCoroutine (Go ());
}
else
{
print ("3");
MeshRendered.enabled = false;
}
}
void PlayAnimation(int clip)
{
animation.clip = animationClip[clip];
animation.Play ();
}
IEnumerator Wait()
{
while(true)
{
yield return new WaitForSeconds(12);
print ("4");
PlayerPrefs.SetInt ("Animation " + WorldCompleted + " Shown", 1);
Start ();
yield return null;
}
}
IEnumerator Go()
{
while(WorldIsCompleted == 1)
{
yield return new WaitForSeconds(startDelay);
startDelay = 0;
animation.Play ();
yield return new WaitForSeconds(delayTime);
}
}
}
I've probably missed out on a bunch of important details, so please ask away. Thank you for reading this also sorry about not having line numbers in, I have no idea how to add them.
Bazzalisk
Answer by dorpeleg · Apr 28, 2015 at 03:07 PM
First, a tip:
store this
"Animation " + WorldCompleted + " Shown"
in a var and reuse it.
Second, where are you trying to play the looping animation? Is it here:
animation.Play();
Because that wont work. You need to tell it what animation to play like you are doing here:
PlayAnimation (0);
I think
animation.Play();
Plays clip at 0 by default.
I love you, this worked. 2 days crying is now finished.