Nullreference exception

Hello,

For some reason, I am getting a null reference exception on the ShowGridPositionList call

  private void UpdateGridVisual()
    {        
        HideAllGridPosition();     

        Unit selectedUnit = UnitActionSystem.Instance.GetSelectedUnit();
        BaseAction selectedAction = UnitActionSystem.Instance.GetSelectedAction();   

        ShowGridPositionList(selectedAction.GetValidActionGridPositionList());
}

It looks like the UnitActionSystem is not running, earlier in the course we had added it to the project settings so it runs first. I am not understanding how it happened or how to fix it.

Which line is throwing the error? Accessing the UnitActionSystem.Instance?
When are you running that code? On the Awake or Start? The Instance should be set on Awake and you should only be accessing the reference on Start
Or maybe it’s the selectedAction that is null? Add a Debug.Log to verify Code Monkey - How to Fix NullReferenceException in C#!

I added some break points, since I am having trouble debugging with Visual Studio Code. I did notice that the UnityActionSystem did load first, it is failing at the third line:
ShowGridPositionList(selectedAction.GetValidActionGridPositionList());

For some reason the selectedAction is null

Check the GetSelectedAction function, maybe you forgot to assign the default Unit reference?

2 Likes

Thanks for the help, I found the issue. It seems as though my reference to the unit got messed up somehow. I reassigned the unit game object to the selected unit serialized field of the UnitActionSystem and that resolved the issue. Not sure how it happened, but I noticed the reference for the Unit was ok, but not the UnitActionSystem.

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

Privacy & Terms