The 'Refactoring' step of Realm Rush sent everything into a wall

By the end of this lesson, I got a NullReferenceException: Object reference not set to an instance of an
object (associated with the EnemyMover script).

There’s a huge hurdle with retracing steps to locate the problem; the code changes in the video barely have any resemblance to the GitLab repository of the project.

After a few hours of minor experimentation, I jumped to attempting to recreate the completed project. This included adding a few new scripts that aren’t attached to anything, so they aren’t creating new errors as far as I can tell. I’ve also changed every Vector2Int to a Vector3Int (with a z variable of zero) to determine if the translation between the two was causing an issue.

The next step is probably to bring the project back to the previous step, but this seems like a good time to ask if I missed something here.

Hi Nakhti,

Welcome to our community! :slight_smile:

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.

If this didn’t help you solve the problem, please share more information on what you did and have in Unity.

Thanks Nina!

I don’t see any exposed fields (aside from a handful that should be empty).

Every error message leads to line 51 in the EnemyMover.cs script, which is the body of this section:

void ReturnToStart()
{
    transform.position = gridManager.GetPositionFromCoordinates(pathfinder.StartCoordinates);
}

If I’m following the logic of what’s happening here, each spawning enemy is creating two errors. When I change the size of the object pool, I get twice the number of errors. But since the enemy starting coordinates are determined by an interplay of several scripts and game objects, this is a tough bug to track down.

In the line of code, I see two variables that might be null: gridManager and pathfinder. Log both variables into your console. Just by looking at this line, it is impossible to tell which one is not referencing any object.

For more context, here’s the full EnemyMover script:

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

[RequireComponent(typeof(Enemy))]
public class EnemyMover : MonoBehaviour
{
[SerializeField] [Range(0f, 5f)]
float speed = 1f;
List path = new List();

Enemy enemy;
GridManager gridManager;
Pathfinder pathfinder;

void OnEnable()
{
    ReturnToStart();
    RecalculatePath(true);
}

private void Awake()
{
    enemy = GetComponent<Enemy>();
    gridManager = FindObjectOfType<GridManager>();
    pathfinder = FindObjectOfType<Pathfinder>();
}

private void Update()
{
    Debug.Log("gridManager: " + gridManager);
    Debug.Log("pathfinder: " + pathfinder);
}

void RecalculatePath(bool resetPath)
{
    Vector3Int coordinates = new Vector3Int();

    if(resetPath)
    {
        coordinates = pathfinder.StartCoordinates;
    }
    else
    {
        coordinates = gridManager.GetCoordinatesFromPosition(transform.position);
    }

    StopAllCoroutines();
    path.Clear();
    path = pathfinder.GetNewPath(coordinates);
    StartCoroutine(FollowPath());
}

void ReturnToStart()
{
    transform.position = gridManager.GetPositionFromCoordinates(pathfinder.StartCoordinates);
}

void FinishPath()
{
    enemy.StealGold();
    gameObject.SetActive(false);

}

IEnumerator FollowPath()
{
    for(int i  = 1; i < path.Count; i++)
    {
        Vector3 startPosition = transform.position;
        Vector3 endPosition = gridManager.GetPositionFromCoordinates(path[i].coordinates);
        float travelPercent = 0f;

        transform.LookAt(endPosition);

        while (travelPercent < 1f)
        {
            travelPercent += Time.deltaTime * speed;
            transform.position = Vector3.Lerp(startPosition, endPosition, travelPercent);
            yield return new WaitForEndOfFrame();
        }
    }

    FinishPath();
}

}

The void Update is just a couple of Debug lines I threw in to test the gridManager and pathfinder variables. They are empty, but I’m not sure if they should be showing a value since they are grabbing other scripts.

I don’t have much experience debugging, so I might be missing something basic.

Alright, I think I figured this out by skipping ahead a few lessons.

I’ve been pulling code from the final build of the game, and trying to bridge the difference. But apparently after ‘Review and Reflect’, some fundamentals change in the game structure. So this is unlikely to work.

If anyone else gets hung up at around this point, I’d suggest either making sure you’re using the right Git version of the code, or coast through until you reach ‘Pathfinding Decisions’.

1 Like

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

Privacy & Terms