- Home /
Question by
Jeredriq · Mar 17, 2015 at 10:43 PM ·
animatorgetcomponentscript error
Script Cant Find Animator
Hi all!
I got this error "Object reference not set to an instance of an Object" . I have my PlayerController script and Animator in same object. Although they're in the same object, my script cant find the animator. And i have a very simple code. I need a help quickly, so i'd appriciate any help. And animator says that its "not initialized".
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private float moveSpeed = 15;
private Vector3 moveDirection;
//bool isMoving = false;
Animator anim;
void Start(){
anim.GetComponent<Animator>();
}
void Update() {
moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")).normalized;
anim.SetFloat("Speed", moveDirection.magnitude);
}
void FixedUpdate() {
rigidbody.MovePosition(rigidbody.position + transform.TransformDirection(moveDirection) * moveSpeed * Time.deltaTime);
}
}
adsız.png
(8.0 kB)
Comment
Answer by giulio-pierucci · Mar 17, 2015 at 10:53 PM
Wrong get component.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
private float moveSpeed = 15;
private Vector3 moveDirection;
//bool isMoving = false;
Animator anim;
void Start(){
anim = gameObject.GetComponent<Animator>();
}
void Update() {
moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical")).normalized;
anim.SetFloat("Speed", moveDirection.magnitude);
}
void FixedUpdate() {
rigidbody.MovePosition(rigidbody.position + transform.TransformDirection(moveDirection) * moveSpeed * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613190448im_/https://answers.unity.com/themes/thub/images/avi.jpg)