NullReferenceException Error

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

In Console, I got these Bug even they are the same with Rick on Github: https://github.com/CompleteUnityDeveloper/06-Laser-Defender/blob/master/Laser%20Defender/Assets/Scripts/EnemyPathing.cs

My script here, thanks you ^o^

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

public class EnemyPathing : MonoBehaviour
{
    WayConfigScripableObj pathData;
    List<Transform> wayPoints;
    int waypointIndex = 0;

    // Start is called before the first frame update
    void Start()
    {
        wayPoints = pathData.GetPathData();
        transform.position = wayPoints[waypointIndex].transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }
    
    public void GetPathData(WayConfigScripableObj pathData)
    {
        this.pathData = pathData;
    }

    private void Move()
    {
        if (waypointIndex <= wayPoints.Count - 1)
        {
            var targetPos = wayPoints[waypointIndex].transform.position;
            var enemySpeedDelta = pathData.GetSpeedOfEnemy() * Time.deltaTime;
            transform.position = Vector2.MoveTowards
                (transform.position, targetPos, enemySpeedDelta);

            if (transform.position == targetPos)
            {
                waypointIndex++;
            }
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

Hi,

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:

Thanks you, now I knew why.

the answer will appear at lesson 117, cause by my test enemy on the screen, just delete that :slight_smile:

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

Privacy & Terms