I think I have found a bug, or at least a very unexpected behaviour in the Animator.
It comes down to this: repeat calls to Animator.Play()
in the same frame are ignored if the requested state is the default state.
Let me break it down. I have a simple state machine like this:
Then, (for good reasons that I will not elaborate) I am calling Animator.Play()
twice in a frame, expecting at least the last call to go through.
Scenario 1: Two calls in one frame.
void Start () {
var animator = GetComponent<Animator>();
int hashA = Animator.StringToHash("A");
int hashB = Animator.StringToHash("B");
int hashDefault = Animator.StringToHash("Default");
animator.Play (hashA); // <-- This is ignored
animator.Play (hashB);
}
I added a logging StateMachineBehaviour to the states, output is this. So far so good.
I called “B” after “A”, so I expect the animator to play “B”, because that was the last call. It “ignores” the call to “A”, that’s fine.
Scenario 2: Second call is default state.
However, under different conditions, where I call “A” then “Default”:
animator.Play (hashA);
animator.Play (hashDefault); // <-- This is ignored, wat?
I get this:
The animator is now not ignoring my first call, but it is “ignoring” the second call and playing “A” in stead of “Default”!
Scenario 3: Waiting a frame
If I wait for a frame, the call does go through.
void Start () {
animator.Play (hashA);
animator.Play (hashDefault); // This is ignored.. :(
StartCoroutine(PlayLater(hashDefault)); // <-- Animator is upset now, he'll do it next frame.
}
IEnumerator PlayLater(int stateHash)
{
yield return null;
GetComponent<Animator>().Play (stateHash);
}
I suspect this has something to do with calling Play()
on the current or on the default state. Is this a bug or is there a rationale for this behaviour? And if it is intentional, how am I supposed to work around it?