Search Unity

Question Changing the state of several animators sequentially in a Coroutine changes them in batches

Discussion in 'Animation' started by dagerob, Mar 29, 2023.

  1. dagerob

    dagerob

    Joined:
    Mar 23, 2018
    Posts:
    3
    Ok, what I mean is I have a List of Custom Scripts that each have a function to change the parameter of an animator. I use this for UI effects so I want to run the "Load animation" in a cascade way, that the objects "load" one after the other. So, I decided to make a Coroutine where I call the RunAnimation() function (that essentially is just a call of anim.SetTrigger()) and then do a yield return new WaitForSeconds(0.2f).

    The problem is that when I try it, the objects are moved in batches so to say. For example if I have 7 objects in the list, the first 2 will move together, then the next 4 together and then the last one, even though I waited inbetween the calls to change the parameters in the animator.

    Here is my code:

    Code (CSharp):
    1. private void LoadPinsSequence(int chapterIndex)
    2.     {
    3.         List<PinController> toLoad = new List<PinController>();
    4.         switch (chapterIndex)
    5.         {
    6.             case 0:
    7.                 toLoad = chapter1Pins;
    8.                 break;
    9.             case 1:
    10.                 toLoad = chapter2Pins;
    11.                 break;
    12.             case 2:
    13.                 toLoad = chapter3Pins;
    14.                 break;
    15.         }
    16.  
    17.         foreach (PinController pin in toLoad)
    18.         {
    19.             pin.RunLoadAnimation();
    20.             yield return new WaitForSeconds(0.2f);
    21.         }
    22.     }
    And the RunLoadAnimation method:

    Code (CSharp):
    1.  public void RunLoadAnimation()
    2.     {
    3.         pinAnimator.SetTrigger("Load");
    4.     }
    Also, the animator state has no exit time, so it should immediately run the animation. Also this only happens with wait times lower than 1 second. I have also tried with WaitForSecondsRealtime(), and also just stalling while measuring the Time.time and elapsed time.

    I also tried with the different Animator update modes in the inspector and nothing changes