0
\$\begingroup\$

The original goal is to switch and play each time animation from the Animator. But not sure if it's possible so i'm trying to play it as animation clips.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class SwitchAnimations : MonoBehaviour
{
    private Animator animator;
    private int index = 0;

    static UnityEditor.Animations.AnimatorController controller;

    // Use this for initialization
    void Start ()
    {
        animator = GetComponent<Animator>();

        GetStateNames(animator);


    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            if (++index == controller.animationClips.Length)
                index = 0;
            Animation clip = controller.animationClips[index];

        }
    }

    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();
    }
}

What i want is to switch between the animation and each time i switch by pressing A it will play the current animation clip.

I'm getting error on the line:

controller.animationClips[index];

Cannot implicitly convert type 'UnityEngine.AnimationClip' to 'UnityEngine.Animation'


Update this is how i'm playing the Animator States:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class SwitchAnimations : MonoBehaviour
{
    private Animator animator;
    private List<AnimationClip> myAnimationComponent = new List<AnimationClip>();
    private int index = 0;
    private static UnityEditor.Animations.AnimatorController controller;
    private UnityEditor.Animations.AnimatorState[] an;

    // Use this for initialization
    void Start()
    {
        animator = GetComponent<Animator>();
        myAnimationComponent = new List<AnimationClip>();

        an = GetStateNames(animator);

        for (int i = 0; i < controller.animationClips.Length; i++)
        {
            myAnimationComponent.Add(controller.animationClips[i]);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            animator.Play(an[index].name);
            if (++index == an.Length)
                index = 0;
        }
    }

    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();
    }
}
\$\endgroup\$
  • \$\begingroup\$ It looks like you edited your question to shift the focus from "This code is not cycling between my animation clips" to "This code is cycling between my animation clips, but now those clips are firing errors on certain animation events" - that's a separate issue, so it should be asked as a separate question. I've clipped out these updates so the answer can be upvoted for successfully solving the original issue. \$\endgroup\$ – DMGregory Feb 9 '18 at 3:20
1
\$\begingroup\$

I would use GetKeyUp when cycling the anims. If you don't want to use the controller in the animation tab you can assign the clips from a array using anim.Play(ac[index].name);

public AnimationClip[] ac;
Animator anim;
int index = 0;
void Start () {
    anim = GetComponent<Animator>();    
}
void Update () {
    if(Input.GetKeyUp(KeyCode.A))
    {       
        anim.Play(ac[index].name);
        if (++index == ac.Length)
            index = 0;        
    }
}
\$\endgroup\$
  • \$\begingroup\$ Can you check please my update ? I edited my question with what i did. I'm getting all the Animator States using the method GetStateNames. Then i'm using array of AnimatorState[] I'm not playing animation clips but the Animator States. Anyway i'm still getting this 3 exceptions that i'm missing receiver!. What is this receiver ? \$\endgroup\$ – Dragon Flea Aug 28 '17 at 22:43
  • \$\begingroup\$ I believe the receiver would be the object to move within the animation. maybe a different name or updated object since the anim was made? To play the state after finding the name you can always use a parameter in the animator then just set it and activate the state. maybe I am still unclear on what exactly you are trying to accomplish. \$\endgroup\$ – Justin Markwell Aug 28 '17 at 23:36
  • \$\begingroup\$ Look here please forum.unity3d.com/threads/… i also added there screenshots to explain what i mean. What i want to do is instead making play for every state in the animator window like anim.Play("walk"); so i need to type the name for each state i'm using the method GetStateNames to get all the states names from the animator and then i want to use the A key to play the states. That is the main goal. In the link i explain there too and also added images of the animator. \$\endgroup\$ – Dragon Flea Aug 28 '17 at 23:46
  • \$\begingroup\$ Later after that will work i will want to get all the animation clips of this object in this case the 'Space_Soldier_A_LOD1' and then play them too using another key. Id i'm not mistaken the animation clips and animator states are not the same thing. So first i want to make the part with the animator states. And in general it's working but i'm getting this exceptions. \$\endgroup\$ – Dragon Flea Aug 28 '17 at 23:48
  • \$\begingroup\$ Ok after reading that. Try the original answer and just add all the animation clips and cycle through them. Most likely will not error. But if you want to first get the state names then you can get those and then a simple forloop to checkthru the ac array and see if ac[index].name == statename and if so then play \$\endgroup\$ – Justin Markwell Aug 28 '17 at 23:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.