I'm trying to use Animator Controller component to transition from Idle animation to walking animation. I can't seem to get animator.SetInteger to work for me. I've tried it with and without the else statement. It works if I put it in the start funtion, but I need it to be tied to the walking input. Movement works fine and through the public float I can see the verticalInput changing when I press the key. I've also tried declaring Animator as public or private.
No compiler errors, but Unity throws this error when I hit play: NullReferenceException: Object reference not set to an instance of an object.
Movement still works fine with play, just no animation unless I change the "Condition" parameter manually through the component.
I also tried Debug.Log(animator.GetInteger("Condition"))
, but I guess that doesn't work like that.
Here's my script:
public class PlayerController : MonoBehaviour
{
public float speed = 8;
public float rotationalSpeed = 45;
public float verticalInput;
private float rotationalInput;
private Animator animator;
void Start()
{
Animator animator = GetComponent<Animator>();
//animator.SetInteger("Condition", 1);
}
void Update()
{
verticalInput = Input.GetAxis("Vertical");
rotationalInput = Input.GetAxis("Horizontal");
transform.Translate(Vector3.forward * verticalInput * speed * Time.deltaTime);
transform.Rotate(Vector3.up * rotationalInput * rotationalSpeed * Time.deltaTime);
if (verticalInput != 0)
{
animator.SetInteger("Condition", 1);
}
else
{
animator.SetInteger("Condition", 0);
}
Debug.Log(animator.GetInteger("Condition"));
}
}
Any Ideas to what I'm doing wrong here?