Get a null reference exception for spinning but not for moving

Hi,

I get a null reference exception when trying to execute the spinning and according to Unity it’s this line that causes the error: selectedUnit.GetSpinAction().Spin(ClearBusy);

But as far as i can see, all the code linked to this line is the same as in the code provided by the course.

I do know how to use the VS debugger by itself but not in conjunction with Unity. Are there some resources available to learn this?

Thanks in advance

I found a youtube search helped me figure out how to connect the two together.

Either selectedUnit is null, or the spin action returned from GetSpinAction() is null. Check these two things

Here’s a simple trick to sort out what may be happening here…
change the above line of code to read:

if(selectedUnit.TryGetComponent(out SpinAction spinAction))
{
     spinAction.Spin(ClearBusy);
} else
{
    Debug.Log($"{selectedUnit} has no SpinAction!");
    ClearBusy();
}

Thanks, i’ll try it out as soon as possible.


Both units seem to not have access to the SpinAction.

Lol, I had forgotten to link the script to the Unit prefab… Guess this ticket can be closed lmao.
Thanks for the support!

1 Like

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

Privacy & Terms