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

I seem to be having the same problem mentioned in two other posts but when I checked my code against the lecture’s update I was not able to identify the mistake. The fields in the Inspector look filled in too. Any suggestions how to fix the issue would be appreciated. Thanks! My EnemyPathing.cs is shown below and image attached.

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

public class EnemyPathing : MonoBehaviour
{
    WaveConfig waveConfig;  //type is WaveConfig (scriptable object)
    List<Transform> waypoints;
    int waypointIndex = 0;  //lists start from zero like arrays

    // Start is called before the first frame update
    void Start()
    {
        waypoints = waveConfig.GetWaypoints();
        transform.position = waypoints[waypointIndex].transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    public void SetWaveConfig(WaveConfig waveConfig)
    {
        this.waveConfig = waveConfig; // "this" refers to waveConfig in this class (EnemyPathing) which
                                      // is assigned the waceConfig value passed through the SetWaveConfig method
    }

    private void Move()
    {
        if (waypointIndex <= waypoints.Count - 1) // lists use Count not Length due to non fixed length
        {
            var targetPosition = waypoints[waypointIndex].transform.position;
            var movementThisFrame = waveConfig.GetMoveSpeed()* Time.deltaTime;
            transform.position = Vector2.MoveTowards
                (transform.position, targetPosition, movementThisFrame); ;
            if (transform.position == targetPosition)
            {
                waypointIndex++;
            }
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

Hi GameGG,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty. Double click on the error message. To which line in your code does it refer?

Check your WaveConfig objects whether they are referencing a path. If they do not contain any variable to which you could have assigned the path, check your enemy and make sure the path in the Hierarchy is connected with its EnemyPathing component.

It is referring to lines 14 and 32 in the EnemyPathing.cs file below. I compared the code to the one in the Resources section and it looks identical.

I checked the enemy and the path in the Hierarchy is connected to the EnemyPathing component from what I can see. Please see below.

As for the WaveConfig objects, if I understand correctly there are 2, the enemyPrefab and the pathPrefab. The former is as I described above. The latter is not referencing a path.
image

Unfortunately, I am unable to address the issues even though I have reviewed the lecture carefully again and seem to be doing all as directed. Any help would be appreciated. Thanks.

Hello. I found out what the issue is by digging deeper in other posts. There was an enemy object in the scene. I deleted that as someone suggested in a post and it worked! I don’t recall Rick saying to do that, but I guess if we’re going to spawn enemies we don’t need it there. Maybe I missed it. Oh well! At least the problem is solved. Thanks anyway.

Good job on solving the problem yourself! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms