Uses For Abstract Classes In Unity

In C#, abstract classes are classes that don’t have a direct implementation. Since you can’t directly instantiate an abstract class, their main use case is as a basis for inheritance. Inheritance can come in pretty handy in Unity if you have many similar but not quite identical game objects. If all you want to change are things like models and animations, you can actually use just one script without any inheritance. It’s when features start to diverge slightly that inheritance starts to come in handy.

Within an abstract class, abstract methods can be defined. By leaving the implementation details of an abstract method up to the inheriting class, you can be sure a feature will be present on your game object while still providing different scripting for that feature on any game object that uses it. It might be that you have one guard you want to have patroling a set of waypoints while another wanders around randomly in a small area. In this case, both guards could inherit the same base class, which contains an abstract method for their movement, allowing you to define how each guard moves separately.

The uses for abstract classes are practically limitless, especially as you start to use them with other features such as protected variables and overrides. Not everything calls for abstract classes, though, so it is important to learn about all core features C# offers so that you can pick the best feature for the particular needs of your situation.

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