Animator not initializing

ok so I’ve got an Animator Controller with a bool of “run” and 2 animations (idle and run). The idle is default and works fine with the model, but my problem is that when i tried to use this script

public Animator Anim;

void Start () {
	Anim = GetComponent<Animator> ();
}

void Update () {
	if (Input.GetKey (KeyCode.W)) {
		Anim.SetBool("run", true);
	} else {
		Anim.SetBool("run", false);
	}
}

}

I keep getting an error “animator is not initialized”. Am i doing something wrong? or did they change something that i need to initialize the animator differently?

Also the script picks up the component of animator when I play the game. So i know the script is grabbing the component of animator. If this is a bug or something then i can reinstall a different version but I’m really confused right about now.

4 Answers

4

Try:

void Start () {
        Anim = this.transform.GetComponent<Animator>();
    }

And as always, if this works for ya, click as answered to the left, and give a thumbs up.

A bit of an old question but I recently had this problem myself. I noticed that sometimes my Animator disables itself which is what causes my animations not to work and this warning pops up. I just enable the Animator when the game starts just in case.

void Start() {
    Anim = GetComponent<Animator>();
    Anim.enabled = true;
}

You need to drag the animator onto the script in the editor and make sure that the bool is spelled correctly.

I have encountered the disabled animator too. Some subtle operations with editing an animation can result in the animator being disabled after you’ve finished. I had to put in a code check for sanity so I get notified when it happens.

I also have traced some animation issues I have had down to the “animator is not initialized” warning. In my case this refers to the fact that the object is not active in it hierarchy. It or one of its parents are not active. When an animation is in this state you cannot set what animation to play or set and triggers, bools or other parameters. They will just get ignored.

I would really hope you could “prep” an object to animate in a certain way before you enable it but it doesn’t allow this right now (am on 5.3.1). You must always have it enabled before you set its animation state.