Tipp: switching selected friendly unit when killed during enemy turn

This course is reall fantastic, thanks Hugo for the intermediate content and the many explanations regarding how things interconnect in c# and Unity. coming from a Java background I found the coding and explantions formidable.

While testing with multiple units I came about a quirk, which I wanted to mention, just in case anybody stumbles acros this:

In case your currently selected unit is killed by an enemy during the enemies turn the UnitActionSystem is not notified and when you start your turn you have a unit selected, which technically does no longer exist. If you try to shoot an enemy in this scenario you will lock inside a busy loop.

This is easily fixed by

  • In the Unit manager checking wether or not when a unit is killed it was the currently selected Unit
  • checking if the friendly Unit List still has at least one entry (otherwhise the game would be over)
  • setting the selected unit to any remainig unit in the freindlyUnitList.

Hope that helps anyone :slight_smile:

6 Likes

Yeah I completely missed this bug since during my testing I never had the selected Unit die.
The solution is indeed like you mentioned, check if the selectedUnit died, if so select the next valid one.
I’m gad you enjoyed the course! Thanks!

4 Likes

Hi,

Any plans to update with a fix for this bug in the future?
It’s been happening a lot to me recently.

Only started coding so I’m struggling getting a fix to work.

1 Like

I have modified the code a bit so i am uncertain if this was in the original. I have a list of units separated by “team” so one for player and one for enemy, when a unit dies i remove them form their corresponding list and the start of the turn just get the unit at position 0 from the player list or enemy list. That should solve that for you.
Another thing i do is which ever unit is acting i set as the selected unit, so there wont be a refence call to a dead unit unless he someone killed himself

1 Like

On the UnitActionSystem listen to the turn changes, when that happens check if the current selectedUnit is dead, if so, get the friendlyUnitList from the UnitManager and find the next available Unit. If none is available, if all are dead, then it’s game over.

3 Likes

I’ll give that a go when I get throught the remainder of the course.
Hopefully I’ll get it working.
Thanks for the reply.

Thanks, I’ll give it a go.

Privacy & Terms