P
P
PlaBetaVer2021-09-21 14:54:15
C++ / C#
PlaBetaVer, 2021-09-21 14:54:15

Should you always use interfaces in Unity?

Do I understand correctly that interfaces should only be used when there are multiple objects that use the same class?

For example, I know that there is only 1 player on the stage that can move using the keyboard elements (WASD / Arrows), then I do not need to create an IMovable interface.
But if there are several objects that the player can interact with, for example, entities that the player can damage and they (entities) will lose a certain amount of health points. Then it would probably be worth adding the IHittable interface for dealing damage (for example, not only the player can deal damage, but also enemies) and IDamageable for taking damage (Damage can be taken by the player and enemies or someone else).
If the entity needs to follow the player, then you need to create an interface IFollow\IFollowable...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-09-21
@GavriKos

Interfaces MAY be used when there are multiple IMPLEMENTATIONS of the same logic.
And your unit-level examples can even be solved with components. And it is possible and interfaces.

N
Nekit Medvedev, 2021-09-30
@NIKROTOS

Nothing is always worth using.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question