Search Unity

GetCurrentAnimatorClipInfo return an empty array

Discussion in 'Animation' started by Kiupe, Jan 6, 2016.

  1. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Hi,

    After starting an animation I try to get the current clip length using the following code :

    Code (CSharp):
    1.  
    2.         animator.SetTrigger ("hide");
    3.        
    4.         //AnimatorStateInfo currentStateInfo = this.animator.GetCurrentAnimatorStateInfo(0);
    5.         //float currentStateInfoLength = currentStateInfo.length;
    6.         //Debug.Log("currentStateInfoLength : " + currentStateInfoLength);
    7.  
    8.         AnimatorClipInfo [] infos = this.animator.GetCurrentAnimatorClipInfo (0);
    9.    
    10.         Debug.Log("infos.Length : " + infos.Length); // infos.Length equals 0
    11.        
    12.         AnimatorClipInfo currentClipInfo = infos[0]; // this line will return an error
    13.        
    14.         AnimationClip currentClip = currentClipInfo.clip;
    15.        
    16.         float currentCliplenght = currentClip.length;
    17.        
    18.         Debug.Log("currentCliplenght : " + currentCliplenght);
    19.  
    The SetTrigger method works as expected and the desired animation is correctly played.

    What is very weird is that this code used to work very well but sometimes it just does not.

    Any idea why ?

    Thank you
     
  2. DontShooot

    DontShooot

    Joined:
    Nov 27, 2012
    Posts:
    4
    Have the same issue. Unity 5.3.1
     
  3. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    After some test I find out that if I wait something like 0.1 seconds then animator.GetCurrentAnimatorClipInfo(0) does return a non-empty array ! It is not very convenient to have to wait before be able to retrieve those kind of information.

    Don't know if it's a bug, but if it's the case I hope it will fix soon.
     
    Last edited: Jan 8, 2016
  4. PestelCrew

    PestelCrew

    Joined:
    Nov 28, 2015
    Posts:
    1
    Bug occurs while animator in transition
     
  5. Tekksin

    Tekksin

    Joined:
    Oct 20, 2013
    Posts:
    32
    use a coroutine to check if anything is in the array before continuing. Like kiupe says, its an error that happens while the animator is in transition, since it has nothing to fill the array with.

    Code (CSharp):
    1.        while (anim.GetCurrentAnimatorClipInfo (0).Length == 0) {
    2.             print ("*******THE ARRAY IS ZERO, LETS WAIT");
    3.             yield return null;
    4.         }
    5.         float f = anim.GetCurrentAnimatorClipInfo (0) [0].clip.length;
    6.  

    Tada :eek:)
     
  6. swingingtom

    swingingtom

    Joined:
    Feb 15, 2018
    Posts:
    10
    Just to share, when `GetCurrentAnimatorClipInfo` returns an empty array while in transition, we can access animation clip infos of the transition with `GetNextAnimatorClipInfo`
     
    deplorablemountaineer likes this.
  7. deplorablemountaineer

    deplorablemountaineer

    Joined:
    Jun 16, 2018
    Posts:
    12
    Yep--this is what I needed to know!