How to get animation name ?

Hello everyone, in my code, i need to get the animation name :

var gun : GameObject;

function Start(){
var animasyon = gun.GetComponent(Animation);
}

function AnimCross(){
animasyon.CrossFade("animationname",0.2);
}

but, i attached this code to a lot of gun, so it’s not working for every weapon. For example:

animasyon.CrossFade("m4a1",0.2);

this only works for the m4a1 weapon, because it only has the animation named m4a1, i want to give that a string value, and make it the name of the animation each gun had, something like that:

function Start(){
var animasyon = gun.GetComonent(Animation);
var animationname : String = gun.Animation.name; ?? 
}

function AnimCross(){
animasyon.CrossFade(animationname,0.2);

}

i want something like that, but couldn’t find the suitable code, can anyone help ?

Thanks :slight_smile:

The Code Below Does 2 Things :

1. Gets all the animation Names and stores them in a string array

2. Plays all animations one time through consecuitvely

There is a boolean in the expression I use it to stop the Update from running other animations, ignoreAnimationLoop Enjoy!

void TestAllAnims() {
		int size = _animation.GetClipCount();
		string[] ani_names = new string;
		int counter = 0;
		foreach (AnimationState states in _animation) {
		ani_names[counter++] = states.name;
		}
		foreach (string s in ani_names) {
		print(s);
		}
		
		StartCoroutine(OneShotAnims(ani_names));
	}
	
IEnumerator OneShotAnims(params string[] animation_names ) {
		ignoreAnimationLoop = true;
		for ( int i = 0 ; i < animation_names.Length ; i++ )
		{
			_animation.wrapMode = WrapMode.Once;
			_animation.PlayQueued(animation_names*,QueueMode.PlayNow);*
  •      do*
    
  •      {*
    
  •          yield return null;*
    
  •      } while ( _animation.isPlaying );*
    
  •  }*
    
  •  ignoreAnimationLoop = false;*
    

}

Hello! A couple of years late hahah but i hope this helps somebody at some point…
If you don’t want to do a script per animation you could simply make public the animation name as a string and type it by hand… however if you want the automatic way you could do this:

public class AnimationHandler : MonoBehaviour {
	List<string> animationNames = new List<string>();
        public float animSpeed=1.0f;
        void Start(){
            foreach(AnimationState states in GameObject.animation){
          	     animationNames.Add(states.name);
            }
        }		               	            
         public void Animate(){		
            //Simply select the index you want from your animation list.
            //(In this case since you want the first one I used the index 0)
		    gameObject.animation.CrossFade(animationNames[0],fadeValue);
     	}
}

As a side note you can access the animation directly using the name like this:

animation["animationName"].speed=0.5f;

Or in our case:

animation[animationNames[0]].speed=0.5f;

The best thing i can tell you is to write up individual scripts for each weapon like the one you have for “m4a1” copy and past the code into each individual weapon beahaviour script and just change the name in the string to the animation for that gun.