Animation Keeps Looping - Why?

Hello,

I have an animation on my scene of a safe door opening. The animation is being controlled from another source, and for some reason, the animation keeps looping. I only want it to play once. Here is a snippet of my script:

var safeDoor : GameObject;

if(codeCorrect == false){
        safeLight.light.color = Color.red;
    }
    else {
        safeLight.light.color = Color.green;
        safeDoor.animation.wrapMode = WrapMode.Once;
        safeDoor.animation.Play("OpenDoorSafe");
    }
}

What am I doing wrong? I also set the WrapMode to Once in the inspecter of the animation file.

Thanks

--- Updated Script ---

private var playAnimationSafeDoor : boolean = true;
var safeDoor : GameObject;

function Update(){

if(codeCorrect == false){
        safeLight.light.color = Color.red;
    }
    else {
        safeLight.light.color = Color.green;
        if(playAnimationSafeDoor == true){
            AnimationSafeDoor();
            playAnimationSafeDoor = false;
        }
    }
}

function AnimationSafeDoor(){
    safeDoor.animation.wrapMode = WrapMode.Once;
    safeDoor.animation.Play("OpenDoorSafe");
    playAnimationSafeDoor = false;
    return;
}

And it still loops ...?

If that's all in an Update call, if codeCorrect happens to be true, the Play call will happen every frame. You'll want to call Play just once.

The problem is cleared now.

I deleted the camera from the scene, then put another new one into it and the problem disappeared.

Before this I tried to copy the project, disable camera, disable all other objects. I don’t know why but there should be some data/metadata created by Unity under the hood and that were deleted only when I removed the entire camera game object.

Would be nice to know if someone has an idea of what happened.

Thanks