Он назначен в редакторе, но при запуске игры во время выполнения контроллер Animator пропадает и больше не назначается.
Я пытаюсь воспроизвести все анимации состояния в контроллере Animator по одной.
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++;
}
}
}
Сначала все переменные в скрипте были приватными, а эта была stati c:
private static AnimatorController controller;
И метод GetStateNames был тоже stati c:
private static AnimatorState[] GetStateNames(Animator animator)
Но я изменил переменную контроллера на publi c, а не stati c, поэтому GetStateNames теперь не stati c, так как скрипт присоединен к пустому GameObject.
Но когда я назначаю Animator Controller контроллеру, при запуске игры его значение sh.
Снимок экрана перед запуском игры:

