You are on page 1of 3

Exercise EventsSOs_01

health, attackDamage, defenseShield are hard coded values so all enemies have the same
values:

* Ways to have different type of enemies?


Check: a different EnemyController() for each type of enemy. Have a base class
Enemy() and inherit from it to have different behaviours (EnemyController01,
EnemyController02…)
Problems: values and behaviours are hard coded.

* What if we want to use the same EnemyController() component for all enemies while having
different types of enemies?
Check: ‘huge’ script containing all the enemy behaviours and select one using a value
contained in a public field. Using an ‘Enum’, for example, to easily select in the Inspector.
So, with a tick in the Inspector, the type is chosen. Or configuring it from outside.
Problems: hard to adjust the values. Need to expose too many parameters in the
Inspector or program a custom Inspector (not trivial). Moreover, these parameters will not keep
their values when stopping the playmode.

* Use SOs (Scriptable Objects).


For example, define an abstract base class for the enemy AI:
and create an asset for each enemy type (Bowser, GlaDOS, Ganon…), where specific
properties or methods can be defined:

And use a general ‘EnemyController’ to be added to each enemy GO:

where the private variables to be used are initialized using the SO values, instead of being hard
coded:

so the enemy type is easily changed just by dragging the SO in the Inspector’s field slot:
And then values can be changed and preserved while in playmode to adjust the game design:

Remember that you can hide a public variable in the inspector, to avoid accidental changes,
using the [HideInInspector] attribute. In the opposite, you can expose a private variable in the
Inspector, using the [SerializedField] attribute.

End of exercise --------------------------------------------

You might also like