Classes and inheritance

Imagine a scenario where you create an Orc class to encode an orc object in the game. Having done so, you then decide to make two upgraded types. One is an Orc Warlord, with better armor and weapons, and the other is an Orc Mage who, as the name implies, is a spell caster. Both can do everything that the ordinary orc can do, but more besides. Now, to implement this, you can create three separate classes, Orc, OrcWarlord, and OrcMage, by copying and pasting common code between them.

The problem is that as Orc Warlord and Orc Mage share a lot of common ground and behaviors with orc, a lot of code will be wastefully copied and pasted to replicate the common behaviors. Furthermore, if you discovered a bug in the shared code ...

Get Mastering Unity Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.