Page 1 of 1

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

Posted: Thu Mar 26, 2020 12:14 am
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?

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

Posted: Thu Mar 26, 2020 10:03 am
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.

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

Posted: Thu Mar 26, 2020 2:09 pm
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.

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

Posted: Thu Mar 26, 2020 2:12 pm
by Tony Li
No problem; glad you got it figured out!