Unity random animation playlist not detecting animations

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

public class MoonPlayRandomAnimation : MonoBehaviour {

    public GameObject MoonPrefab;

    public List <AnimationClip> anim = new List <AnimationClip>();


    void Update ()

    {
        StartAnimation ();
    }


    public void StartAnimation ()

    {
        int rand = Random.Range ( 0 , anim.Count );
        Debug.Log (rand);
        MoonPrefab.GetComponent<Animation> ().Play ( anim [rand].name );
    }





}

I researched this code online and it seemed very straight forward, but for whatever reason Unity refuses to detect the animations I have dragged into the list, any ideas why?

Here’s the Debug.Log :

I would try this to see if it works:

Animation moonAnimation;
void Start() {
   moonAnimation = MoonPrefab.GetComponent<Animation>();
 }
public void StartAnimation ()
{
    int rand = Random.Range ( 0 , anim.Count );
    moonAnimation.clip = anim[rand];
    moonAnimation.Play();
    Debug.Log (rand);
}

Since it takes a string, I believe the animation clips must be on the animations (or list of) must be on the Animation component to be used like that.
This way, you’ll assign from your list to the clip and just use ‘play’ because it should play the default.

Hmm, I’ve tried some things, but for whatever reason it’s still not getting the animation clips, I have the animation component on the prefab for sure yet I still get the same error message. Just to show you what’s happening I’ll post up a screenshot as you can see, the animations are definitely there.

I put the script onto an empty just to be sure I wasn’t doing anything daft, hasn’t made a difference, oddly enough, I did check out a playlist for audio tracks and that works absolutely fine.

Interesting. After several minutes of trying to get this to work, I finally got it working just before giving up.
I had to change to legacy animations (for me anyways), and I had a list on 1 script, and the same list on the animation of another game object. I tried to remake what I think you’re doing…

Anyways, the sanest option, I think, is to use the animator instead. :slight_smile:

However, the code I posted is what I used and it works. Just that I had to make sure to have the clips on the animations component as well, but it seems you already have that.

I tried getting the animation controller method working using a tutorial that explained things well but it’s annoying me a bit.

I think I might have tried looking up how to even change animations to ‘legacy’ but I have no idea about it. Since the method before was actually working for me, would you mind telling me how to change my animations to legacy to get everything working?

Sure, I had to turn the inspector to ‘debug mode’ and then you’ll see an option for legacy.

If you used the Animator, I believe all you’d need is your various states. If you’re not caring about transitions, you could store a string array with the names of the states (animations), and just do “anim.Play(animations[index])”.

Oh of course! Thanks! I’ll check all of this out.

Hmm this is strange, I’m back onto the original code, there are now no longer any errors happening with the the legacy animations but the animations aren’t playing either.

I just came across something very strange! I thought something weird was going on with no errors happening. When I use the animation component and put my animations in and change them to legacy as @methos5k suggested which you need to do in order to make the playlist code work I ended up checking out my prefab and the animations had somehow eaten themselves.

Not kidding, the sprites were somehow gone from the clips and I have no idea why, my spritesheets are all safe however but this was a bit alarming, I may have to give the game controller option a try again despite the struggle. Why is it so damn hard just to set up a random animation playlist in Unity? I had no problems setting up an audio playlist which I’m using for my music.

Sorry, not really sure about all that… Can’t hurt to try the Animator if this is too much hassle. :slight_smile:

Gahhhhh -_- lol

I’ve almost got it working just after taking the time to look through the tutorial, for whatever reason though it seems to be ignoring one of my animations despite them all being connected up.

Okay, nevermind I’ve pretty much got it working now just following the tutorial it’s a very odd way of doing things especially compared to an audio playlist but it all works.

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

public class MoonPlayRandomAnimationController : StateMachineBehaviour {

    public string m_ParameterName = "MoonID";
    public int [] m_StateIDArray = { 0, 1, 2 };
 

    override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)

    {
        if (m_StateIDArray.Length <=0)

        {
            animator.SetInteger ( m_ParameterName, 0 );
        }

        else

        {
            int index = Random.Range (0, m_StateIDArray.Length);
            animator.SetInteger (m_ParameterName, m_StateIDArray [index]);
        }
    }

}

The only issue I’m having now is just the timing of everything, I think I want a delay for the next animation so perhaps a coroutine would work here. Ohhhh it feels so nice to get this issue out of the way lol.