Search Unity

Animation Controller using public functions

Discussion in 'Scripting' started by Proto-G, May 12, 2021.

  1. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    I'm wondering how to create a C# animation controller with an index of animations that can be played with public functions. I would like to populate all of my animations into the inspector and play any one of them using a public function via a UI button or event in the inspector. Thanks :)
     
  2. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    This compiles, but doesn't play the animation. I populated the public animation list with a few animations. The

    Console says: default clip could not be found in attached animations list.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayAnimations : MonoBehaviour
    6. {
    7.     public GameObject thisObject;
    8.     public AnimationClip[] animList;
    9.     Animation anim;
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         anim = thisObject.GetComponent<Animation>();
    15.     }
    16.  
    17.     public void Play0()
    18.     {
    19.         anim.clip = animList[0];
    20.         anim.Play();    
    21.     }
    22. }
    23.  
     
  3. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    It actually does work, but the GameObject must be the original object that the animations are attached to. They also must be Legacy. I'm using a humanoid and want to use random humanoid animations. How would I go about this?