Animations in player can't find state, but work in editor

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Animations in player can't find state, but work in editor

Post by VoodooDetective »

This is probably a stupid question, but I'm getting this error in the player:

Code: Select all

invalid layer index '-1'
When I try to play an animation. The same AnimatorPlay() call works in the editor. Have you run across that before?
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Animations in player can't find state, but work in editor

Post by Tony Li »

Hi,

Does the animation still work?

If not, check these things:
  • The AnimatorPlay() command specifies the correct subject.
  • The animator state name is correct.
  • The subject has an Animator component with an animator controller that has that state name.
Internally, the AnimatorPlay() sequencer command calls the Animator component's Play() or CrossFadeInFixedTime() method.

You can specify a layer in your animator controller. For example, to specify a cross-fade of 0.3 seconds and layer 1:

Code: Select all

AnimatorPlay(Idle, speaker, 0.3, 1)
If you don't specify a layer, it defaults to layer '-1' which, to quote the API: "If layer is -1, it plays the first state with the given state name or hash."

If there is no state with the name you specified, Unity will report that error.
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Animations in player can't find state, but work in editor

Post by VoodooDetective »

Ah ok, thanks for the information.

So I did some sleuthing and it looks like it was my fault :(
I had two actors in the same registered as my main character (I have an Actor prefab). I guess in editor, one object registered first, and in player another object registered first. Sorry about the silly question.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Animations in player can't find state, but work in editor

Post by Tony Li »

No problem; glad you got it figured out!
Post Reply