NullReferenceException: Object reference not set to an instance of an object

Hi i have been trying to fix this but i have no idee what’s wrong

NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.SetAnimatorMovement (UnityEngine.Vector2 direction) (at Assets/Scripts/PlayerMovement.cs:52)
PlayerMovement.Move () (at Assets/Scripts/PlayerMovement.cs:25)
PlayerMovement.Update () (at Assets/Scripts/PlayerMovement.cs:19)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour {

public float speed;
private Vector2 direction;
private Animator animator;

void start()
{
animator = GetComponent();
}

void Update()
{
TakeInput();
Move();
}

private void Move()
{
transform.Translate(direction * speed * Time.deltaTime);
SetAnimatorMovement(direction);
}

private void TakeInput()
{
direction = Vector2.zero;

if(Input.GetKey(KeyCode.W))
{
direction += Vector2.up;
}
if (Input.GetKey(KeyCode.A))
{
direction += Vector2.left;
}
if (Input.GetKey(KeyCode.S))
{
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.D))
{
direction += Vector2.right;
}
}

private void SetAnimatorMovement(Vector2 direction)
{
animator.SetFloat(“xDir”, direction.x);
animator.SetFloat(“yDir”, direction.y);
}

}

please help

5922074–632843–PlayerMovement.cs (1.16 KB)

Use code tags…

Otherwise, the error message points to SetAnimatorMovement. And since animator is the likely culprit, that is probably null.

The reason it is null is because your Start is lowercase. start vs Start isn’t the same.

2 Likes

Thank you so much. I only started yesterday so i still don’t know anything.