How can I add an animation to a mesh that I created dynamically?

I need to create some walls on the fly and can add scripts to these walls usint AddComponent. I cannot understand how to add an animation to it. I have some animations that I made in the project folders and tried to add one like this.

var anim : Animation = newCurvedWall.GetComponent(Animation); = "LeftSpeed3";

This does not hive errors but it also does not animate. These animations work if I drag onto a prefab or mesh in the Hierarchy, but I need to do it in script. How should I do this? is the same as or just name

Try anim.Play("LeftSpeed3");