Modular Scripts Versys Interfaces In Unity

Ben Pielstick
2 min readJun 16, 2021

In programming there are is usually more than one way to solve a problem. Unity has an interesting opportunity space when it comes to components, and what each C# script is responsible for. Taking a look at something like health, it would be possible to create an interface that contains a health property and a damage method, which could be implemented by multiple entities. It would also be possible to create a health script and add it to each entity via a script component.

There really isn’t a best answer here, as far as I can tell. Interfaces are more flexible, if you need different logic for the same feature on each entity, but want a standard way for external scripts to interact with it. Modular scripts are more powerful because you can build anything you want once, and reuse it several times similarly to inheritance, but there is no limit on how many script components you can use, so if you have a lot of standard code, breaking it up into multiple scripts you can then use to compose an entity from makes a lot of sense.

Composing entities out of several script components is something that might not be immediately obvious, since it is a function of how GameObjects are structured in Unity rather than an inbuilt function of C#, but it is something you should definitely something to be aware of if you’re looking at making games in Unity. As usual, it is best to know all the options that are available and pick the right one for your particular situation.

--

--

Ben Pielstick

Game developer with over 12 years of experience, mainly focused on MMOs.