Help with Animator has not been initialized error

Every time I try to run the game I get the error: Animator has not been initialized.
UnityEngine.Animator:SetFloat(String, Single)

Here’s the portion it’s calling the error on:

anim.SetFloat(“Speed”, speed);
anim.SetFloat(“Move”, isMoving);

And here’s the whole script:

public class PlayerMovement : MonoBehaviour
    {
        public float speed = 1f;
        public Vector3 touchPoint;
    	public float isMoving = 1f;
    	
	public Animator anim; 
    void Update()
    {
		anim.SetFloat("Speed", speed);
		anim.SetFloat("Move", isMoving);
		
        // Lerps to the last position touched, going to have to modify this
        // to only work when tapped not hold cause shooting will be hold.
        
        if (Input.touchCount > 0 )
        {
            // Shoots a ray where you touched the screen
            RaycastHit hit;
            // There was an error before. The tag must be main camera.
            Ray ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
            
            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
                // converts the touch point to unitys 3d worldspace Vector3
                touchPoint = hit.point;                                                                         
            }
        }
        MoveToPos(touchPoint);
		//isMoving = (Mathf.Abs(transform.position.z - touchPoint.z)* 10);
    }
    void MoveToPos(Vector3 touchPoint)
    {          
        gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(touchPoint.x, 10, touchPoint.z), speed * Time.deltaTime);	// LAWL this had to be z even tho im using y for droid , wasted alot of time on that.  
        //gameObject.transform.LookAt(touchpoint);
    }
}

Animator not ready in this frame, use it in next frame.
you can use coroutine to do this:

yield return new WaitForEndOfFrame();
obj.GetComponent().SetBool(“run”, true);

I had this error and found that the controller reference on my Animator component had been set to null somehow. Re-adding the reference to the Controller fixed the issue.

I had this error and struggled for a few hours, but then I added:

“anim = GetComponentAnimator>();”

in the update function and it seems to work, but I am not sure why…

you defined “anim” but never set it to anything.

try:

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

This will set “anim” to the Animator component on the game object.

I had the same issue with unity 5.0 but this was due to my stupidity.

Make sure you are referencing to the correct object. I had object referenced from inspector to my script and I was calling getComponent on that object. For testing I just replaced the object which didn’t have Animator component attached to it.