Losing Animation Reference

I have a c# script on a character and get a reference to the animation in start() like so:

bat = GameObject.Find("BBPB_animations").GetComponent<Animation>() ["Take 001"];

I then call idleStart() from start() which sets the animation frame to the idle loop portion. That works fine.

I have an animation event at the end of the idle loop that calls idleStart() again to set the time to the start of the idle loop again. The method is called - but I am losing the reference to bat animation.

void idleStart()
    {
        Debug.Log ("idleStart() bat " + bat);
        bat.speed = 1f;
        bat.normalizedTime = (1f / 729f) * 516f;
    }

First time in when called from start() I get bat UnityEngine.AnimationState in the console… but when called a second later from the event I get null and a NullReferenceException.

I can’t figure out why I lose the reference.

Ack. Me being dumb. Inadvertently had a second copy of the script running…

1 Like