No unit selected when I run the game

When I start the game I get a null error because there is no unit selected when the game starts. So I’m using a workaround solution for that:
In the UnitActionSystemUI.CreateUnitActionButtons() i add this line after declaring the selected Unit
Unit selectedUnit = UnityActionSystem.Instance.GetSelectedUnit();
if (selectedUnit == null) return; // The new workaround

It seems like Codemonkey has a unit selected when the game runs and I don’t. What am I missing?

It always helps to ask a question, because you usually find the solution right after the question was asked. :slight_smile:

To anyone in the same position: If you want a standard unit to be selected when the game starts, you need to add it to the UnitActionSystem GameObject. If there is no standard unit selected, the game will throw a null exception error and the workaround for that error is adding if (selectedUnit == null) return; right below Unit selectedUnit = UnityActionSystem.Instance.GetSelectedUnit(); in the CreateUnitActionButtons() :slight_smile:

Yup the default one is the one referenced in the SerializeField
Careful with that test for null, the game is designed to assume that a Unit is always selected. If you want to allow the player to have no unit selected then a bunch of things need to be changed.

Yeah I agree with you. I’m a little further along in the course now and I see that my solution checking for the null error isn’t going to work. So… Yeah, I messed up and the solution is to of course to add a unit to the serialized field :slight_smile:

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

Privacy & Terms