2

I'm getting a NullReferenceException on this line:

animator.transform.position = Vector2.MoveTowards(animator.transform.position, patrolPoints[randomPoint].transform.position, speed * Time.deltaTime);

How can I fix this?

Here's my code:

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

public class PatrolBehaviour : StateMachineBehaviour
{
    private GameObject[] patrolPoints;

    public float speed;

    int randomPoint;

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        patrolPoints = GameObject.FindGameObjectsWithTag("patrolPoints");
        randomPoint = Random.Range(0, patrolPoints.Length);
    }

    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
       animator.transform.position = Vector2.MoveTowards(animator.transform.position, patrolPoints[randomPoint].transform.position, speed * Time.deltaTime);

        if (Vector2.Distance(animator.transform.position, patrolPoints[randomPoint].transform.position) < 0.1f)
        {
            randomPoint = Random.Range(0, patrolPoints.Length);
        }
    }

    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {

    }

}
Donut
  • 110,061
  • 20
  • 134
  • 146
Isaac-James Nel
  • 29
  • 1
  • 1
  • 2

1 Answers1

5

Well the error "object reference not set to an instance of an object" means that you are trying to access something that is currently empty. To try and fix this first be sure that all of your variables have set values and are not null.

Jahan
  • 98
  • 7
  • Hi Jahan, i have tried to replace my code with the suggested code you gave. However the same error occurs. If this help im following a Udemy course by BlackThornProd if you have heard of it. I'm up to the boss animating and coding stage. The boss has an inrto, run and walk animaton. This script that i'm working on is a behaviour on one of the animations in the animator window (The walk animation). what the boss character is supposed to do is walk from random patrolPoints to another random patrolPoint however this error is effectinmg this. – Isaac-James Nel Oct 29 '20 at 20:51
  • @Isaac-JamesNel Does it say which line of code the error came from? – Jahan Oct 29 '20 at 20:57
  • yes, the error always occurs in this line of code " { animator.transform.position = Vector2.MoveTowards(animator.transform.position, patrolPoints[randomPoint].transform.position, speed * Time.deltaTime);" – Isaac-James Nel Oct 29 '20 at 21:01
  • try replacing it with `override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { animator.gameObject.transform.position = Vector2.MoveTowards(animator.gameObject.transform.position, patrolPoints[randomPoint].transform.position, speed * Time.deltaTime);` and if that doesn't work I recommend trying to make default versions of each variable (like `int defVariable = default;) to find out exactly which variable is causing the error. – Jahan Oct 29 '20 at 21:04
  • Actually nevermind the code I gave you was unnecessary as you can still access transform from the animation compenent. – Jahan Oct 29 '20 at 21:06
  • I'd recommend just trying to debug it to see which variable is causing the error and if that doesn't work double check your code with the person's whos course your watching's code. For some debugging tips I recommend checking out [this link](https://michaelscodingspot.com/debugging-part1/). – Jahan Oct 29 '20 at 21:14
  • Alright, thanks for the help. I've continued finishing the boss 'chase or run' state and that works very well (the boss chases after its health is halved). so i might just re-do that whole entire walk animation and see if that works because the run animation with very similar code works great. – Isaac-James Nel Oct 29 '20 at 21:43
  • I get this error whenever I start a unity project. I am not sure how to fix it. – Alexander Soiefer May 01 '23 at 01:33