Animation keeps looping even on WrapMode.Once;

Why does my animation keep looping over and over even when the Wrap Mode is set to Once?
I attached the animation using the Animation component on my object’s inspector, and the wrap mode there is also set to Once.

Here is my current code to play the animation:

void Update () {

//If missileBool is TRUE, then play the animation only ONCE.

if(missileBool)
	{
		animation["missileAnim"].wrapMode = WrapMode.Once;
		animation.Play("missileAnim");
	}

//This keeps looping the animation though.

}

Thanks!

The animation will keep playing until you set the missileBool to false.

The reason is that as per the Unity Documentation for Animation.Play

If the animation is already playing,
other animations will be stopped but
the animation will not rewind to the
beginning. If the animation is not set
to be looping it will be stopped and
rewinded after playing.

So in your case it checks for missileBool to be true in each frame and initiates the animation but since it is already playing the animation simply resumes as per,

“If the animation is already playing,
other animations will be stopped but
the animation will not rewind to the
beginning.”

And then after is stops it is restarted since the missileBool is true and animation has already stopped and it needs to be played since the missileBool is true.

Try code below:

if(missileBool)
    {
       animation["missileAnim"].wrapMode = WrapMode.Once;
       animation.Play("missileAnim");
       missileBool = false;
    }