Unity 2018 - Composition vs. Inheritance

"In Unity, GameObjects are constructed through composition rather than classical inheritance."


Inheritance allows classes to take on properties of other existing classes. This is helpful in preventing code repetition and general organization. Traditionally in game dev, the enemy class is an extension of a base class (NPC or something). The different varieties of enemies are all then extended from the enemy superclass.

Enemy Inheritance Diagram

To create a swordsman enemy you would just need the SwordFightingEnemy class added to the GameObject. All the needed behaviors (AI, health, etc.) would already be inherited from the superclasses.


Unity uses a different method called Composition to add functionality to GameObjects. For composition, each piece of functionality is split up into classes which can be modularly added of a GameObject.

For example, let’s use the enemies from the inheritance example, ones that shoot projectiles and ones that use swords.

Enemy Inheritance Diagram

Three different types of enemies are created from only three different components (Enemy, FireProjectile, and SwordFighting) as compared to the inheritance method that uses four components ( Enemy, ShootingEnemy, ShootingAndSwordfightingEnemy, and SwordFightingEnemy).

This modular approach makes it really easy to share functionality between objects and quickly generate different variations. For more information about the subject, check out the Wiki.


Published 20 Nov 2018

General Consultant To All Fields of a Mathematical Nature. Currently exploring game development, machine learning, and various programmatical oddities.
Chad Lumley on Twitter