I have a GameObject that adds an AnimationEvent to a clip that calls a function at completion of the animation. It calls this function reliably – until I destroy the game object. I destroy the object when I detect a collision and I am careful to call Animation.Stop() before I destroy the object, so the end function callback should not be called, but appears to be. My worry is that Unity might still try to call the function even if I stop the animation early, but by that time the object is gone.
The relevant code:
function BeginIdleAnimation() {
if (idleAnimation) {
var repeat : boolean = idleAnimation.wrapMode == WrapMode.Loop
|| idleAnimation.wrapMode == WrapMode.PingPong;
PlayAnimationClip(idleAnimation, repeat ? "IdleAnimDone" : null);
PlayClip(idleSound);
}
}
function PlayAnimationClip(clip : AnimationClip, endFunc : String) {
if (clip) {
// create event that calls desired endFunc when animation completes
if (endFunc) {
var event : AnimationEvent = new AnimationEvent();
event.functionName = endFunc;
event.time = clip.length;
clip.AddEvent(event);
}
animation.clip = clip;
animation.Play();
}
}
// Here's the relevant snippet of collision code that supposedly stops the animation before destroying itself...
function OnCollision(p : Projectile) {
if (p) {
animation.Stop();
Destroy(transform.parent.gameObject);
}
}
function IdleAnimDone() {
PlayClip(idleSound);
}
I get “AnimationEvent ‘IdleAnimDone’ has no receiver!” error.
Thanks in advance!