Reputation: 880
I am trying to do a simple animation with a simple state machine, but Unity prints out an error message:
Invalid Layer Index '-1' UnityEngine.Animator:Play(String, Int32, Single)
I have verified that my animator state machine is located in the "Base Layer", which should have the ID = -1 by default.
In addition, I have printed out all the layer IDs in my games, and they are all "-1", and I am not sure if that is the correct answer because I thought that each layer should have a unique (or different) ID ?
However, regardless, Unity still prints out the same error above unfortunately.
I am using the latest version of Unity 5.5.2
Here is the code:
using UnityEngine;
public class Thanh_Space_Man : MonoBehaviour {
public Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown("1"))
{
anim.Play ("Run_Rifle_Foreward", -1, 0f);
}
if(Input.GetKeyDown("2"))
{
anim.Play ("Idle_Rifle_01", -1, 0f);
}
}
}
FYI: I am a beginner in Unity. :-)
Upvotes: 1
Views: 8612
Reputation: 1
I had same problem. The reason was that I added a new object with a new animator but forgot to change in the Awake:
_myAnimator = FindObjectOfType<Animator>(); // just one animator in scene
to
_myAnimator = GetComponent<Animator>(); // own animator of that object
After changing those lines for both objects everything worked fine.
Upvotes: 0
Reputation: 5108
These are the values I get when using LayerMask.NameToLayer where "CustomLayer" is a layer I created manually in the Tags & Layer manager.
Debug.Log(LayerMask.NameToLayer("asd")); // => -1
Debug.Log(LayerMask.NameToLayer("UI")); // => 5
Debug.Log(LayerMask.NameToLayer("CustomLayer")); // => 8
Debug.Log(LayerMask.NameToLayer("customlayer")); // => -1
Debug.Log(LayerMask.NameToLayer("Default")); // => 0
Update:
OP found the issue:
I've just figured out that the error is that I was incorrectly spelled the name of the animation state when I called Animator.Play(). After I use the correct name of the state, then it works fine. It is even more interesting that regardless of whether I pass in the Layer ID as 0 or -1, when I call Animator.Play(), it works in both cases.
Upvotes: 1