Only on first select, no matter what I do (I’ve copied and pasted the actual file for UnitActionSystem), still throwing this error prior to selection. Post selection works great. Any ideas?
Can you show UnitActionSystem.cs
? The error says the exception is on line 36 but there is nothing on line 36 in the file for this lecture
Thanks so much for checking - I’ve progressed since then, which moved it a line, but it was the highlighted. For some reason the return is not stopping it from reaching the line selectedUnit.Move(MouseWorld.GetPosition());
I had some debugs in and it seemed like it was working correctly. The error only fires on the first selection if you’re clicking not on a unit. After you’ve selected one, or if you select one correctly, it works fine.
TryHandleUnitSelection()
sets the selected unit if you clicked on one, but your code then exits the Update()
method. If you didn’t click on a unit, selectedUnit
will still be null and the code then continues to try an ‘move’ the unit that was never selected, resulting in your error. Looking at the code for this lecture, Hugo set the selected unit in the inspector so his was never null. I wouldn’t worry too much about it because all of this code is going to change, but if you want you could either do what Hugo did and set the selected unit in the inspector (I can’t see yours so I don’t know if it’s serialised) or you could add a guard. It would be something like this
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (TryHandleUnitSelection()) return;
if (selectedUnit != null) selectedUnit.Move(MouseWorld.GetPosition());
}
}
Ideally, null checking selectedUnit should always be a part of your process here.
Super helpful. Thank you!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.