I’m writing all this maybe someone in the future has the same error and was trying the same setup. It doesn’t matter for the topic of the thread. it is some random error unity gives but it also gives some related errors. I didn’t used unity as intended so I this is not a bug just the feedback from unity that is not clear. so one can skip the text bellow if it one has anything better to do than read stuff on forums.
I needed to find out at runtime how long an animation is inside the Animation Controller WHEN the animation name is NOT THE SAME as the state name.
The idea is to have controllers on different prefabs that share the same state structure BUT the animations clips will be different for each prefab. These animations clips will also have different lengths.
Then by C# script I can ask the controller to play a state AND get the animation length using a few lines of code.
private AnimatorController animCtrl;
private ChildAnimatorState[] animatorStates;
animCtrl = animator.runtimeAnimatorController as AnimatorController;
animatorStates = runtimeAnimCtrl.layers[0].stateMachine.states;
I have only one layer inside the controller so I just need to get all the states from that layer.
After this just need to loop trough states and get the clip length from whatever state I need.
//when stateName is "Attack" for example
foreach (var state in animatorStates)
{
if (state.state.name == stateName)
{
var stateClip = state.state.motion as AnimationClip;
return stateClip.length;
}
}
This off course works perfectly fine inside the editor but will not work at runtime because ChildAnimatorState and AnimatorController are part of UnityEditor.Animations
And will not work at runtime because the UnityEditor stuff is not included in the game
Ok so finally unity will try to build the project but because it finds these lines of code it fails the build. The error I’ve got about not being able to write a text file was misleading. It is offcourse a error on my side not paying attention of what libraries can be used or not.
For the solution on how to get the animation length at runtime I’m trying to find it right now but each I see is not “satisfactory”
First I could use the code in the editor to populate some list attached to a prefab that has all the clips names attached to each prefab. Which could work, maybe, but then I need to remember to run this script for each prefab I will have, I also don’t trust unity saving stuff in the editor because prefabs or scriptable objects will sometimes get their data wiped out at random by unity.
There is also manual way of attaching events to an animation timeline which will fire when the animation reaches that point BUT these will not let me know in advance WHEN this animation point will be reached to plan around this before the animation will start playing. So I can react when the animation will end but I will not know how long the animation is.
There are also Animator Behaviors with scripts attached to each state. I don’t know if it gives me the animation length
philosophically It is idiotic to do all these steps when the AnimationController should give me this info when I need it.
“How long is the animation attached to the state Attack?”
“It is 2.5 my lord.”
But nope, we can’t have that at runtime.