It is assigned in the editor but when running the game in run time the Animator controller is gone and not assigned anymore.
I'm trying to play all the state animations in Animator controller one by one.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEditor.Animations;
public class SwitchAnimations : MonoBehaviour
{
public AnimatorController controller;
private Animator animator;
private AnimatorState[] states;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
states = GetStateNames(animator);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StartCoroutine(QueueAnim(states));
}
}
private AnimatorState[] GetStateNames(Animator animator)
{
controller = animator ? animator.runtimeAnimatorController as AnimatorController : null;
return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
}
IEnumerator QueueAnim(params AnimatorState[] anim)
{
int index = 0;
while (index < anim.Length)
{
if (index == anim.Length)
index = 0;
animator.Play(anim[index].name);
AnimatorStateInfo si = animator.GetCurrentAnimatorStateInfo(index);
yield return new WaitForSeconds(5);
index++;
}
}
}
First all the variable in the script was private and this one was static :
private static AnimatorController controller;
And the method GetStateNames was static too :
private static AnimatorState[] GetStateNames(Animator animator)
But I changed the controller variable to be public and not static so the GetStateNames is not static now since the script is attached to empty GameObject.
But when I assign Animator Controller to the controller it's vanish when running the game.
Screenshot before running the game :
It appears you are assigning an AnimatorController and not an Animator. An Animator references/uses an AnimatorController but not the other way around. Just make Animator public and assign it the AnimatorController.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments