Object reference not set to an instance of an object

So I watched a tutorial on how to make an A.I walk, chase and attack. Everything was working fine until I got to the chase scene which resulted in the warning of “NullReferenceException: Object reference not set to an instance of an Object”, so what I want this script to do, is when the player reaches the sightRange value I’ve inputted, it’ll run the “Chase” method and proceed to go after the player. But whenever I test my game and go up to my A.I character, that object reference error bit started popping up. For my player controller I’m using Unity’s third person starter asset, for the layer which is what the code is using for reference, I’ve just added in the Player layer to the PlayerArmature object as that’s the only object with changing positions, I didn’t change the rest of the children and just it’s parent overall.

Note: Added in debug logs since before it wasn’t doing anything at all, after tweaking some stuff within the nav mesh agent and nav mesh surface, the nullreference bit started popping up.

My referenced code:

GameObject player;

NavMeshAgent agent;

[SerializeField] LayerMask groundLayer, playerLayer;

//Patrol
Vector3 destPoint;
bool walkpointSet;
[SerializeField] float range;

Animator animator;

//State change
[SerializeField] float sightRange, attackRange;
bool playerInSight, playerInAttackRange;
void Start()
{
agent = GetComponent();
player = GameObject.Find(“Player”);
}

void Update()
{
playerInSight = Physics.CheckSphere(transform.position, sightRange, playerLayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, playerLayer);

if (!playerInSight && !playerInAttackRange) { Patrol(); }
if (playerInSight && !playerInAttackRange) { Chase(); }
if (playerInSight && playerInAttackRange) { Attack(); }
}
void Patrol()
{
if (!walkpointSet) SearchForDestination();
if(walkpointSet) agent.SetDestination(destPoint);
if (Vector3.Distance(transform.position, destPoint) < 10) walkpointSet = false;

}

void Chase()
{
agent.SetDestination(player.transform.position);
Debug.Log(“Chase method executed”);
}

void Attack()
{
if (!animator.GetCurrentAnimatorStateInfo(0).IsName(“AttackingSigbin”))
{
animator.SetTrigger(“Attack”);
agent.SetDestination(destPoint);
Debug.Log(“Attack method executed”);
}
}

void SearchForDestination()
{
float z = Random.Range(-range, range);
float x = Random.Range(-range, range);

destPoint = new Vector3(transform.position.x + x, transform.position.y, transform.position.z + z);

if(Physics.Raycast(destPoint, Vector3.down, groundLayer))
{
walkpointSet = true;
}
}

Please wrap your code in tags properly. It makes it easier for others to help you out.
To which line of code does it mention null reference exception?

using UnityEngine;
using UnityEngine.AI;
public class NewBehaviourScript : MonoBehaviour
{
    GameObject player;
    NavMeshAgent agent;
    [SerializeField] LayerMask groundLayer, playerLayer;
    Vector3 destPoint;
    bool walkpointSet;
    [SerializeField] float range;
    Animator animator;
    [SerializeField] float sightRange, attackRange;
    bool playerInSight, playerInAttackRange;
    private void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        player = GameObject.Find("Player");
    }
    private void Update()
    {
        playerInSight = Physics.CheckSphere(transform.position, sightRange, playerLayer);
        playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, playerLayer);

        if (!playerInSight && !playerInAttackRange) { Patrol(); }
        if (playerInSight && !playerInAttackRange) { Chase(); }
        if (playerInSight && playerInAttackRange) { Attack(); }
    }
    private void Patrol()
    {
        if (!walkpointSet) SearchForDestination();
        if (walkpointSet) agent.SetDestination(destPoint);
        if (Vector3.Distance(transform.position, destPoint) < 10) walkpointSet = false;

    }
    private void Chase()
    {
        agent.SetDestination(player.transform.position);
        Debug.Log("Chase method executed");
    }
    private void Attack()
    {
        if (!animator.GetCurrentAnimatorStateInfo(0).IsName("AttackingSigbin"))
        {
            animator.SetTrigger("Attack");
            agent.SetDestination(destPoint);
            Debug.Log("Attack method executed");
        }
    }
    private void SearchForDestination()
    {
        float z = Random.Range(-range, range);
        float x = Random.Range(-range, range);

        destPoint = new Vector3(transform.position.x + x, transform.position.y, transform.position.z + z);

        if (Physics.Raycast(destPoint, Vector3.down, groundLayer))
        {
            walkpointSet = true;
        }
    }
}

Mb I’m new, but actually I’ve already found my problem for this after digging in a bit more, just posted this early in case I don’t find anything and I’ll at least have this thread prepared, thanks for replying though!

1 Like

Good to hear you solved it. Don’t forget to tag/prefix your thread as solved.

For all people finding this in the future:

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that
1 Like