Chad
Lumley
Sr. Front-End Engineer

Unity 2018 - Composition vs. Inheritance

I recently discovered I was writing my Unity code the 'Old Fashoned' game developer way by using classical inheritance. Unity was built on the foundation of using Composition, a modular way to build objects. Let's talk about the differences.

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

Inheritace

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
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.

Composition

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
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.

-CL


Written by: Chad Lumley

Consultant to all problems of a mathematical nature. Currently exploring game development, machine learning, and teaching cats to talk.

© 2021 Chad Lumley, Built with // Gatsby