using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchAnimations : MonoBehaviour
{
private Animator animator;
private int index = 0;
public string[] animations = new string[] {"Grounded", "Aiming",
"Roll", "Use", "PickupObject", "Any State", "Death_A"};
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.A))
{
if (++index == animations.Length)
index = 0;
animator.Play(animations[index]);
}
}
}
But i’m getting index -1 exception after pressing some times on A: Animator.GotoState: State could not be found and Invalid Layer Index ‘-1’
And also two errors that i’m missing a receiver not sure how to fix it too.
‘Space_Soldier_A_LOD1’ AnimationEvent ‘CantRotate’ has no receiver! Are you missing a component?
So i wonder where is the problem and i found more animations inside a blend tree. This is the RifleActions layer:
But then if i click for example double click on Grounded i’m getting into this area:
What i want is to switch between all animations from the Animator by pressing on A.
The solution is to add this exceptions to the script as methods. Even empty methods but they should be in the script since this animations having this events so there must be methods with this events names.
Instead messing up with the animations and events the easiest way is to add empty methods. And later if you need you can use this methods for example in RollSound you can play audio clip. But the main idea to solve it is to add this methods. Here is the complete code:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Animations;
using UnityEngine;
public class SwitchAnimations : MonoBehaviour
{
private Animator animator;
private static UnityEditor.Animations.AnimatorController controller;
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 static UnityEditor.Animations.AnimatorState[] GetStateNames(Animator animator)
{
controller = animator ? animator.runtimeAnimatorController as UnityEditor.Animations.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++;
}
}
private void RollSound()
{
}
private void CantRotate()
{
}
private void EndRoll()
{
}
private void EndPickup()
{
}
}