Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Chocolade, Jan 7, 2019.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    The warning is on the line:

    Code (csharp):
    1.  
    2. animator.Play(randClip.name);
    3.  
    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)
    <PlayRandomly>d__7:MoveNext() (at Assets/My Scripts/Animations/PlayAnimations.cs:71)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using System.Linq;
    6. using UnityEngine;
    7.  
    8. public static class IListExtensions
    9. {
    10.     /// <summary>
    11.     /// Shuffles the element order of the specified list.
    12.     /// </summary>
    13.     public static void Shuffle<T>(this IList<T> ts)
    14.     {
    15.         var count = ts.Count;
    16.         var last = count - 1;
    17.         for (var i = 0; i < last; ++i)
    18.         {
    19.             var r = UnityEngine.Random.Range(i, count);
    20.             var tmp = ts[i];
    21.             ts[i] = ts[r];
    22.             ts[r] = tmp;
    23.         }
    24.     }
    25. }
    26.  
    27. public class PlayAnimations : MonoBehaviour
    28. {
    29.     public Animator animator;
    30.     private AnimationClip[] clips;
    31.     private List<AnimationClip> clipsList = new List<AnimationClip>();
    32.     private string[] names;
    33.     private bool wasInitialized;
    34.  
    35.     private void Awake()
    36.     {
    37.         // Get all available clips
    38.         clips = animator.runtimeAnimatorController.animationClips;
    39.  
    40.         names = File.ReadAllLines(@"c:\temp\names.txt");
    41.         for (int i = 0; i < clips.Length; i++)
    42.         {
    43.             string name = clips[i].name;//.Substring(clips[i].name.IndexOf(".Animation"));
    44.             if (clips[i].name.Contains("mixamo"))
    45.             {
    46.                 clipsList.Add(clips[i]);
    47.             }
    48.         }
    49.     }
    50.  
    51.     public void Init()
    52.     {
    53.         // Only start the coroutine if not initialized yet
    54.         if (!wasInitialized && clipsList.Count > 0)
    55.         {
    56.             wasInitialized = true;
    57.             StartCoroutine(PlayRandomly());
    58.         }
    59.     }
    60.  
    61.     private IEnumerator PlayRandomly()
    62.     {
    63.         while (true)
    64.         {
    65.             clipsList.Shuffle();
    66.  
    67.             foreach (var randClip in clipsList)
    68.             {
    69.                 animator.Play(randClip.name);
    70.                 yield return new WaitForSeconds(randClip.length);
    71.             }
    72.         }
    73.     }
    74. }
    75.  

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

    on Line:

    Code (csharp):
    1.  
    2. playanimation.Init();
    3.  

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class AnimatorController : MonoBehaviour
    7. {
    8.    public Animator[] animators;
    9.    public Transform target;
    10.    public float speed = 1f;
    11.    public float rotationSpeed;
    12.    public bool slowDown = false;
    13.    public PlayAnimations playanimation;
    14.  
    15.    private bool endRot = false;
    16.    private Vector3 center;
    17.  
    18.    // Use this for initialization
    19.    void Start()
    20.    {
    21.        center = target.GetComponent<Renderer>().bounds.center;
    22.  
    23.        for (int i = 0; i < animators.Length; i++)
    24.        {
    25.            animators[i].SetFloat("Walking Speed", speed);
    26.        }
    27.    }
    28.  
    29.    // Update is called once per frame
    30.    void Update()
    31.    {
    32.        float distanceFromTarget = Vector3.Distance(animators[2].transform.position, target.position);
    33.  
    34.        for(int i = 0; i < animators.Length; i++)
    35.        {
    36.            animators[2].transform.position = Vector3.MoveTowards(animators[2].transform.position, center, 0);
    37.        }
    38.  
    39.        if (slowDown)
    40.        {
    41.            if (distanceFromTarget < 10)
    42.            {
    43.                float speed = (distanceFromTarget / 10) / 1;
    44.                for (int i = 0; i < animators.Length; i++)
    45.                {
    46.                    animators[i].SetFloat("Walking Speed", speed);
    47.                }
    48.            }
    49.        }
    50.  
    51.        if (distanceFromTarget < 5f)
    52.        {
    53.            for (int i = 0; i < animators.Length; i++)
    54.            {
    55.                //animators[i].SetFloat("Walking Speed", 0);
    56.                animators[i].SetBool("Idle", true);
    57.                playanimation.Init();
    58.            }
    59.  
    60.            if (!endRot)
    61.            {
    62.                Quaternion goalRotation = Quaternion.Euler(0f, 0f, 0f);
    63.                float angleToGoal = Quaternion.Angle(
    64.                        goalRotation,
    65.                        animators[0].transform.localRotation);
    66.                float angleThisFrame = Mathf.Min(angleToGoal, rotationSpeed * Time.deltaTime);
    67.  
    68.                // use axis of Vector3.down to keep angles positive for ease of use
    69.                animators[0].transform.Rotate(Vector3.up, angleThisFrame);
    70.                animators[1].transform.Rotate(Vector3.down, angleThisFrame);
    71.  
    72.                // We end if we rotated the remaining amount.
    73.                endRot = (angleThisFrame == angleToGoal);
    74.            }
    75.            {
    76.                animators[0].SetBool("Rifle Aiming Idle", true);
    77.                animators[1].SetBool("Rifle Aiming Idle", true);
    78.            }
    79.        }
    80.    }
    81. }
    82.  
    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.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    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);
     
    Chocolade likes this.
  3. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    Tried it now and the result in the editor log is:

    mixamo.com
    UnityEngine.Debug:Log(Object)
    <PlayRandomly>d__7:MoveNext() (at Assets/My Scripts/Animations/PlayAnimations.cs:68)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
     
    Antypodish likes this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    So I assume, you have now realized, what is your issue?
     
    Chocolade likes this.
  5. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    The randClip.name is not valid name ? It's object instead animationclip ?
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    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.
     
    Chocolade likes this.
  7. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    932
    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.
     
    Antypodish likes this.