Search Unity

Question Animator - IndexOutOfRangeException when debugging Animator.GetCurrentAnimatorClipInfo(0).clip.name

Discussion in 'Animation' started by MlleBun, Oct 7, 2020.

  1. MlleBun

    MlleBun

    Joined:
    Sep 19, 2017
    Posts:
    163
    Hi everyone,

    I get an Animator - IndexOutOfRangeException: Index was outside the bounds of the array when debugging Animator.GetCurrentAnimatorClipInfo(0).clip.name in the Update method. Could anyone guide me through this?

    The code is put on the gameObject holding the Animator component and I just want to know at each frame in which State the character is.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerLocomotion : MonoBehaviour
    4. {
    5.     public Animator animator;
    6.     private AnimatorClipInfo[] currentClip;
    7.  
    8.     private void Start()
    9.     {
    10.         animator = GetComponent<Animator>();
    11.     }
    12.  
    13.     private void Update()
    14.     {
    15.         // Debug
    16.         currentClip = animator.GetCurrentAnimatorClipInfo(0);
    17.         Debug.Log(currentClip[0].clip.name)
    18.     }
    19. }
    20.  
    Many thanks!