Why I'm getting warning in the editor invalid layer index -1 when trying to play animation clips ?

The warning is on the line:

animator.Play(randClip.name);

After filtering there are 12 clips in the List clips.
I’m getting the same warning each time it’s trying to play a clip.

The warning is:

Invalid Layer Index ‘-1’
UnityEngine.Animator:Play(String)
d__7:MoveNext() (at Assets/My Scripts/Animations/PlayAnimations.cs:71)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public static class IListExtensions
{
    /// <summary>
    /// Shuffles the element order of the specified list.
    /// </summary>
    public static void Shuffle<T>(this IList<T> ts)
    {
        var count = ts.Count;
        var last = count - 1;
        for (var i = 0; i < last; ++i)
        {
            var r = UnityEngine.Random.Range(i, count);
            var tmp = ts[i];
            ts[i] = ts[r];
            ts[r] = tmp;
        }
    }
}

public class PlayAnimations : MonoBehaviour
{
    public Animator animator;
    private AnimationClip[] clips;
    private List<AnimationClip> clipsList = new List<AnimationClip>();
    private string[] names;
    private bool wasInitialized;

    private void Awake()
    {
        // Get all available clips
        clips = animator.runtimeAnimatorController.animationClips;

        names = File.ReadAllLines(@"c:\temp\names.txt");
        for (int i = 0; i < clips.Length; i++)
        {
            string name = clips[i].name;//.Substring(clips[i].name.IndexOf(".Animation"));
            if (clips[i].name.Contains("mixamo"))
            {
                clipsList.Add(clips[i]);
            }
        }
    }

    public void Init()
    {
        // Only start the coroutine if not initialized yet
        if (!wasInitialized && clipsList.Count > 0)
        {
            wasInitialized = true;
            StartCoroutine(PlayRandomly());
        }
    }

    private IEnumerator PlayRandomly()
    {
        while (true)
        {
            clipsList.Shuffle();

            foreach (var randClip in clipsList)
            {
                animator.Play(randClip.name);
                yield return new WaitForSeconds(randClip.length);
            }
        }
    }
}

This is the script I’m calling the method Init to start playing the animations:

on Line:

playanimation.Init();
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimatorController : MonoBehaviour
{
   public Animator[] animators;
   public Transform target;
   public float speed = 1f;
   public float rotationSpeed;
   public bool slowDown = false;
   public PlayAnimations playanimation;

   private bool endRot = false;
   private Vector3 center;

   // Use this for initialization
   void Start()
   {
       center = target.GetComponent<Renderer>().bounds.center;

       for (int i = 0; i < animators.Length; i++)
       {
           animators[i].SetFloat("Walking Speed", speed);
       }
   }

   // Update is called once per frame
   void Update()
   {
       float distanceFromTarget = Vector3.Distance(animators[2].transform.position, target.position);

       for(int i = 0; i < animators.Length; i++)
       {
           animators[2].transform.position = Vector3.MoveTowards(animators[2].transform.position, center, 0);
       }

       if (slowDown)
       {
           if (distanceFromTarget < 10)
           {
               float speed = (distanceFromTarget / 10) / 1;
               for (int i = 0; i < animators.Length; i++)
               {
                   animators[i].SetFloat("Walking Speed", speed);
               }
           }
       }

       if (distanceFromTarget < 5f)
       {
           for (int i = 0; i < animators.Length; i++)
           {
               //animators[i].SetFloat("Walking Speed", 0);
               animators[i].SetBool("Idle", true);
               playanimation.Init();
           }

           if (!endRot)
           {
               Quaternion goalRotation = Quaternion.Euler(0f, 0f, 0f);
               float angleToGoal = Quaternion.Angle(
                       goalRotation,
                       animators[0].transform.localRotation);
               float angleThisFrame = Mathf.Min(angleToGoal, rotationSpeed * Time.deltaTime);

               // use axis of Vector3.down to keep angles positive for ease of use
               animators[0].transform.Rotate(Vector3.up, angleThisFrame);
               animators[1].transform.Rotate(Vector3.down, angleThisFrame);

               // We end if we rotated the remaining amount.
               endRot = (angleThisFrame == angleToGoal);
           }
           {
               animators[0].SetBool("Rifle Aiming Idle", true);
               animators[1].SetBool("Rifle Aiming Idle", true);
           }
       }
   }
}

This is my Animator settings:
The main layer have two states in use: Walk and Idle. Both using HumanoidWalk and HumandoidIdle.

Then when it’s getting to the Idle in the script it should start playing the states animations inside
Magic StateMachine:

And when using a break point in the PlayAnimations script inside the while loop on the foreach loop I see that clips contains the animation clips from the sub statemachine: Magic StateMachine

But I’m getting this warning/s and it’s not playing the animations in Magic StateMachine.

Put Debug.Log before this line 69 (it may be in your code actually 68)
And check in the debug log if your random clip name is valid.
animator.Play(randClip.name);

1 Like

Tried it now and the result in the editor log is:

mixamo.com
UnityEngine.Debug:Log(Object)
d__7:MoveNext() (at Assets/My Scripts/Animations/PlayAnimations.cs:68)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

1 Like

So I assume, you have now realized, what is your issue?

1 Like

The randClip.name is not valid name ? It’s object instead animationclip ?

Its value is mixamo.com.
So I don’t know where that comes from. You need dig it out.
But you use coroutines, which are not good for beginners.
They easily can introduce issues, which I am not getting int, nor bother analyse.

1 Like

The problem was with the name. I renamed the animation/s names to something else without a dot and it’s working now.

Thank you.

1 Like