- Home /
Question by
SmashingSuccess · Jul 27, 2016 at 07:15 PM ·
animationanimatoranimator controller2d animationinitialization
Attempting to change AnimatorController during Runtime
I am attempting to tie animations to specific weapons in Unity, such that when you switch weapons you also switch AnimationControllers. However, every time I try to switch them out the animator says the controller is "None(Runtime Animator Controller)", and throws up an endless stream of "Animator has not been initialized" errors. Here is the code I am using:
public class CreatureAttack : MonoBehaviour {
protected Animator myAnimator;
protected RuntimeAnimatorController myAnimationController;
// Use this for initialization
protected virtual void Start ()
{
myAnimator = GetComponent<Animator>();
myAnimationController = myAnimator.runtimeAnimatorController;
}
// Update is called once per frame
protected virtual void FixedUpdate() {
if (Input.GetKeyDown(KeyCode.V))
{
Debug.Log("This should work");
myAnimator.runtimeAnimatorController = (RuntimeAnimatorController)Resources.Load("Animation/TestController.controller", typeof(RuntimeAnimatorController));
myAnimator.PlayInFixedTime("Idle");
}
}
}
The Debug line is showing up, and the controller I am trying to access is called TestController, so I am not entirely sure what is going on here. Is there any way of fixing this problem?
(Also, I have tried with and without the PlayInFixedUpdate line, and the results are identical.)
Comment