NullReferenceException on Show

When I use the show function from Testing.cs I get the error NullReferenceException: Object reference not set to an instance of an object Testing.Update () (at Assets/Scripts/Testing.cs:25)

Here is the function

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.T))
        {
            GridSystemVisual.Instance.HideAllGridPositions();
            GridSystemVisual.Instance.ShowGridPosition(unit.GetMoveAction().GetValidActionGridPositionList());
        }
    }

I have rewatched the video 3 times trying to make sure I attacked all of the parts to the difference elements and I cannot see anywhere I messed up. Sorry I’m having to ask for help on every lesson.

Did it work before you added the GridSystemVisual singleton? I don’t know which of the lines you shared is line 25 and there are several things that could be null here;

  • The GridSystemVisual may be broken and therefore not returning an instance
  • The unit is not set in the inspector
  • The unit does not have a MoveAction

Check all of these

Yes, the code worked before this lesson.

Line 25 is

GridSystemVisual.Instance.ShowGridPosition(unit.GetMoveAction().GetValidActionGridPositionList());

The unit does have MoveAction.cs with it.

MoveAction.cs

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

public class MoveAction : MonoBehaviour
{
   private Vector3 targetPosition;
   [SerializeField] private Animator unitAnimator;
   [SerializeField] private int maxMoveDistance =4;
   Unit unit;
   private void Awake()
    {
        unit = GetComponent<Unit>();
        targetPosition = transform.position;
    }
    

    void Update()
    {
        float stoppingDistance = .1f;
        if (Vector3.Distance(transform.position, targetPosition) > stoppingDistance)
        {
            Vector3 moveDirection = (targetPosition - transform.position).normalized;
            float moveSpeed = 4f;
            transform.position += moveDirection * moveSpeed * Time.deltaTime;

            float rotateSpeed = 10f;
            transform.forward = Vector3.Lerp(transform.forward, moveDirection, Time.deltaTime * rotateSpeed);

            unitAnimator.SetBool("IsWalking", true);
        } else
        {
            unitAnimator.SetBool("IsWalking", false);
        }
    }

    public void Move(GridPosition gridPosition)
    {
        this.targetPosition = LevelGrid.Instance.GetWorldPosition(gridPosition);
    }

    public List<GridPosition> GetValidActionGridPositionList()
    {
        List<GridPosition> validGridPositionList = new List<GridPosition>();
        GridPosition unitGridPosition = unit.GetGridPosition();

        for (int x = -maxMoveDistance; x <= maxMoveDistance; x++)
        {
            for (int z=-maxMoveDistance; z <= maxMoveDistance;z++)
            {
                GridPosition offsetGridPosition = new GridPosition(x,z);
                GridPosition testGridPosition = unitGridPosition + offsetGridPosition;
                
                //checks for valid grid position
                if(!LevelGrid.Instance.IsValidGridPosition(testGridPosition))   {continue;}
                //checks to see if this is the same grid square the unit is currently in
                if(unitGridPosition == testGridPosition){continue;}
                //checks to see if the grid square is occupied
                if(LevelGrid.Instance.IsGridOccupied(testGridPosition)){continue;}

                validGridPositionList.Add(testGridPosition);
                

            } 
        }

        return validGridPositionList;
    }

    public bool IsValidActionGridPosition(GridPosition gridPosition)
    {
        List<GridPosition> validGridPositionList = GetValidActionGridPositionList();
        return validGridPositionList.Contains(gridPosition);
    }
}
  • The GridSystemVisual may be broken and therefore not returning an instance - ok, so we’ve passed the HideAllGridPositions() suggesting the singleton worked.
  • The unit is not set in the inspector - Still don’t know if there’s a unit assigned in the inspector, so this could still be null.
  • The unit does not have a MoveAction - We have a screenshot of the MoveAction on the unit. Sweet, not that.
  • Also don’t know if unit.GetMoveAction() is returning the move action, so adding this point

Blockquote * The unit is not set in the inspector - Still don’t know if there’s a unit assigned in the inspector, so this could still be null.

So, what do you need to see to confirm this?

Blockquote* Also don’t know if unit.GetMoveAction() is returning the move action, so adding this point

here is the GetMoveAction() from Unit.cs

    public MoveAction GetMoveAction()
    {
        return moveAction;
    }

Check your test component in Unity. You should have a unit dragged into the Unit field in the inspector.

And moveAction has been set in Awake()?


Edit
The quickest would probably be to change your test code to this and check the console

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.T))
        {
            GridSystemVisual.Instance.HideAllGridPositions();
            Debug.Asset(unit, "Unit is null");
            Debug.Assert(unit.GetMoveAction(), "MoveAction is null")
            GridSystemVisual.Instance.ShowGridPosition(unit.GetMoveAction().GetValidActionGridPositionList());
        }
    }

That got it! I failed to assign the unit in Testing.cs. Now it works on the first Unit but not the second.

Yes, it won’t work on the second because the test script isn’t looking at the second. It’s only looking at the unit you dragged into the field

1 Like

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

Privacy & Terms