adding a clip to a animation component via script

Is it possible to do this? I get an error message telling me that “Property UnityEngine.component.animation is Read-Only”. Does this mean that you cannot alter the animation component’s properties via scripting?

Here’s my script so far:

@script RequireComponent(Animation)
  

function Start () {

animation.animation = "EnemyAdvanceLeft";
}

function Update () {

}

My gut is telling me that Unity probably doesn’t know where to find the animation called “EnemyAdvanceLeft”. It’s just that the error message I received does not indicate that’s the primary issue.

Thanks.

You need to add the actual clip - with animation.AddClip().

Here’s my C# script for adding all of the animations in a folder:

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

[AddComponentMenu("System/Load Animations")]
public class LoadAnimations : MonoBehaviour {

	public new string name;
	
	void Awake()
	{
		var clips = Resources.LoadAll("Animations/" + name, typeof(AnimationClip)).Cast<AnimationClip>();
		foreach (var c in clips)
		{
			animation.AddClip(c, c.name.Contains("@") ? c.name.Substring(c.name.LastIndexOf("@") + 1) : c.name);
		}

		foreach (var a in animation.Cast<AnimationState>())
		{
			a.enabled = true;
		}
	}
	
}

OK, thanks. Well, I loaded the C# script via the Immediate Window in MonoDevelop.

Then I tried to add the animation using the following script which I attached via code to the game object I had instantiated. It failed, and I got the following error: “No appropriate version of ‘UnityEngine.Animation.AddClip’ for the argument list ‘(UnityEngine.AnimationClip)’ was found.”

Here’s my script:

@script RequireComponent(Animation)

function Start () {
var enemyAdvanceLeft : AnimationClip;
animation.AddClip(enemyAdvanceLeft);

}

function Update () {

}

Any idea what I’m doing wrong? Thanks again.

In the end, I ended up assigning both possible scripts to each prefab, then simply assigning the one to be played via script. This means that each instance of the prefab carries an animation it does not need, but I found this to be simpler than trying to assign it during run-time, as I am still getting error messages when I try to do this.

Thanks again Mike for your help.

Check this out awesome example GitHub - tsarikovskiy/Unity3D-DynamicallyLoadingAnimation: 👾 Unity3D Loading and unloading animations at runtime (Example)

If you don’t want to mark your animations as legacy, you can use the new Playables API to play non-legacy AnimationClips:

private List<PlayableGraph> graphs = new List<PlayableGraph> ();

// Just call this function when you want to play an AnimationClip on a specific GameObject.
// from https://docs.unity3d.com/Manual/Playables-Examples.html
private PlayableGraph playAnim(AnimationClip clip, GameObject obj) {
    PlayableGraph playableGraph;

    AnimationPlayableUtilities.PlayClip(obj.AddComponent<Animator>(), clip, out playableGraph);

    // save all graphs we create and destroy them at the end of our scene.
    // you might need to optimize this if you make a lot of animations.
    graphs.Add (playableGraph);

    return playableGraph;
}

void OnDisable() {
    foreach (var g in graphs) {
        g.Destroy();
    }
    graphs.Clear ();
}