Am working on Unity and here is what I want to do: play the animationType in a time difference of 10sec. I want the code to loop through the animations and play them each for 10seconds. The code runs without errors, except the result is not what I expected it to be. It plays the first animation,Boxing, for 10 seconds and just when it's about to play the Backflip animation, it starts to do some weird thing to the character. That's where is goes wrong.
Here is my code:
public class BeBot_Controller : MonoBehaviour
{
Animator anim;
string animationType;
string[] split;
int arrayLength;
void Start()
{
//AndroidJavaClass pluginClass = new AndroidJavaClass("yenettaapp.my_bebot_plugin.My_BeBot_Plugin");
//animationType = pluginClass.CallStatic<string>("getMessage");
animationType="Null,Boxing,Backflip";
split = animationType.Split(',');
anim = gameObject.GetComponentInChildren<Animator> ();
arrayLength = split.Length;
}
// Update is called once per frame
void Update () {
if (arrayLength > 1){
StartCoroutine ("LoopThroughAnimation");
}
}
IEnumerator LoopThroughAnimation()
{
for (int i = 1 ; i < arrayLength; i++) {
animationType = split [i];
//anim.SetInteger ("AnimPar", 0);
anim.Play (animationType);
yield return new WaitForSeconds (10);
}
}
}
So what did I do wrong here ? Is there any other way I can solve this problem ?
Since your animation loop only needs to be called once, simply move StartCoroutine() to Start() and remove the Update() stuff:
public class BeBot_Controller : MonoBehaviour
{
private Animator anim;
private string animationType;
private string[] split;
private int arrayLength;
void Start ()
{
//AndroidJavaClass pluginClass = new AndroidJavaClass("yenettaapp.my_bebot_plugin.My_BeBot_Plugin");
//animationType = pluginClass.CallStatic<string>("getMessage");
animationType = "Null,Boxing,Backflip";
split = animationType.Split(',');
anim = gameObject.GetComponentInChildren<Animator>();
arrayLength = split.Length;
// Call here
StartCoroutine(LoopThroughAnimation());
}
IEnumerator LoopThroughAnimation ()
{
for (int i = 1; i < arrayLength; i++)
{
animationType = split[i];
Debug.Log(animationType);
//anim.SetInteger ("AnimPar", 0);
anim.Play(animationType);
yield return new WaitForSeconds(10);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With