SpinAction GetValidActionGridPosition returns null

Hey there,
I’m getting a null exception when trying to get the grid position of the unit in the SpinAction class.
Here’s the error:

NullReferenceException: Object reference not set to an instance of an object
SpinAction.GetValidActionGridPositionList () (at Assets/Scripts/Actions/SpinAction.cs:50)
GridSystemVisual.UpdateGridVisual () (at Assets/Scripts/Grid/GridSystemVisual.cs:56)
GridSystemVisual.Update () (at Assets/Scripts/Grid/GridSystemVisual.cs:46)

Basically, I think the unitGridPosition is not getting anything from unit.GetGridPosition in SpinAction.

Here’s the code I have:

public override List<GridPosition> GetValidActionGridPositionList()
    {
        GridPosition unitGridPostion = unit.GetGridPosition();

        Debug.Log(unitGridPostion);
        return new List<GridPosition> { unitGridPostion };
    }

I added the Debug.Log for testing, but that’s pretty much the code block from the lecture. The same line in MoveAction does return the unit’s GridPosition, but let me at least show the relevant BaseAction lines too.

BaseAction implementation:

public virtual bool IsValidActionGridPosition(GridPosition gridPosition)
    {
        List<GridPosition> validGridPositionList = GetValidActionGridPositionList();
        return validGridPositionList.Contains(gridPosition);
    }

    public abstract List<GridPosition> GetValidActionGridPositionList();

I’ve double-checked the reference scripts on the GitHub and tried to comb through my scripts to see if I can track it down, but I have had no luck. I feel like it’s got to be obvious, but I’m just overlooking it.

I figured out my issue. I doubt anyone else will stumble across my issue, but here’s the scoop just in case you did what I did.

The issue stemmed from how I tackled the “make the unit spin only once” challenge a few lectures back. I had used an Awake() method in my SpinAction class, which needed to be updated after we made the BaseAction class, which became the following:

protected override void Awake()
    {
        isActive = false;
    }

And the reason my SpinAction class had no clue what was the unit’s position, was that I was overwriting the BaseAction awake:

protected virtual void Awake()
    {
        unit = GetComponent<Unit>();
    }

I had blindly corrected the error for the Awake method that was created when I had SpinAction inherit BaseAction.

Like I said, I doubt anyone else runs into this issue, but just in case someone else runs into it, I thought I’d share.

In this case, you could have written your SpinAction Awake() like this:

protected override void Awake()
    {
        base.Awake();
        isActive = false;
    }

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

Privacy & Terms