Error NullReferenceException

this error appeared and I couldn’t find a solution. Visual Studio does not point out any errors or alerts.
error:
NullReferenceException: Object reference not set to an instance of an object
EnemyPathing.Move () (at Assets/Scripts/EnemyPathing.cs:34)
EnemyPathing.Update () (at Assets/Scripts/EnemyPathing.cs:25)
my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyPathing : MonoBehaviour
{
WaveConfig waveConfig;
List wayPoints;

int wayPointIndex = 0;


// Use this for initialization
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;
}
private void Move()
{
    if (wayPointIndex <= wayPoints.Count - 1)
    {
        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 Igor,

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.


See also:

Hi Nina,
This is the line that unity reports the error…

NullReferenceException: Object reference not set to an instance of an object
EnemyPathing.Move () (at Assets/Scripts/EnemyPathing.cs:34)
EnemyPathing.Update () (at Assets/Scripts/EnemyPathing.cs:25)

Move(); this line inside void Update

and this line;
if (wayPointIndex <= wayPoints.Count - 1)
inside private void Move

In inspector i can´t find any issues

it´s resolved unchecking the enemypathing in path prefabs!

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

Privacy & Terms