1
\$\begingroup\$

My animation is working but unity keep saying that my Animator has not been initialized error Here my Code:

using UnityEngine;
using System.Collections;

public class Player: MonoBehaviour {

public float maxspeed = 10f;
bool facingRight = true;

Animator anim;

bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;

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

void FixedUpdate () {
    grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    anim.SetBool ("Ground", grounded);



    float move = Input.GetAxis ("Horizontal");
    anim.SetFloat ("Speed", Mathf.Abs (move));

    GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxspeed, GetComponent<Rigidbody2D>().velocity.y);
    if (move > 0 && !facingRight){
        Flip ();
    } else if (move < 0 && facingRight) {
        Flip ();
    }
}

void Flip()
{
    facingRight = !facingRight;
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;    
}

}

\$\endgroup\$
1
\$\begingroup\$

This has been discussed a few times in Unity's forums. This thread has a long discussion about the issue, and this and this Unity answers posts.

If you don't solve it and everything seems to work fine, I suggest submitting a bug to Unity. Remember this is a warning, so it is possible that even though you see it everything will work well. Usually it happens if you are accessing an animator that is disabled or immediately after enabling it. This doesn't seem the case from your code though. Another thing to try is to close Unity and reopen it, sometimes this goes away.

\$\endgroup\$
0
\$\begingroup\$

We had the same problem but finally we solved it. The problem was the gameobject which holds the Animator component must be active on the scene before you set the variables.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.