C# Inheritance in Unity! – Intermediate Scripting Tutorial

C# Inheritance in Unity! – Intermediate Scripting Tutorial


– [Narrator] The scripting languages that Unity supports have a
feature called Inheritance. Inheritance is one of the cornerstones of objected oriented programming, or OOP for short. When a class inherits from another, it gains the features of
the class it inherits from. When talking about inheritance, the class that is being inherited from is called the parent or base class. The class that is inheriting is called the child or derived class. The result of inheritance is that items that exist in the parent class will also be available in the child class, therefore, methods and variables can be used in the child class as if it was the parent class. For example, assume you have
a parent class called Class A, which contains two
methods, dance and sing. If you have another class, Class B, which inherits from Class A, then Class B will also have the two
methods, dance and sing. These methods do not need to be created in Class B because they
already exist in Class A. When dealing with inheritance, there are three access
modifiers to be aware of, public, private, and protected. You should already be familiar with the concepts of the public and the private access modifiers. Just be aware that the features of a parent class that are public will exist and be accessible
in the child class, while features that are private will exist but not be
accessible in the child class. The protected access modifier acts as a hybrid between public and private. All features of a parent class that are protected will
exist and be accessible in a child class, like public features, but will not be accessible outside of the parent or child
classes, like private features. It is likely that most of the classes you have used so far in
Unity have been inheriting. Indeed, all scripts which are applied as components to game
objects are Monobehaviors. This means they have inherited from the Monobehavior class. By default, scripts made in
Unity follow this format. Public class followed by
the name of the class, followed by a colon and the
class name Monobehavior. The colon in class name, Monobehavior, is telling the script that it
inherits from Monobehavior. To make this class inherit
from another class, simply change the name Monobehavior to some other class name. To change the class so
that it doesn’t inherit from any parent class,
simply remove the colon and parent class name. You might be wondering
why our scripts inherit from Monobehavior. Items like game object, transform, the start method, the update method, and more, all come from Monobehavior. Our scripts inherit from Monobehavior so that we have access to these features. The inheritance structure is hierarchical. A common way to think of inheritance is to think of the animal kingdom. In this example, we’d have a
parent class called animal. This class would contain
all of the definitions and properties necessary to make the class behave like an animal. From this animal base class, we might have a couple of child classes,
vertebrate and invertebrate. The vertebrate class would then, in turn, be the parent class for more classes such as mammal, reptile, or amphibian. Each of these child classes would take the information given by its base class and add to it. Just like our animal example, inheritance in object oriented programming is known as an IS-A relationship. This means that the child
class is a parent class. A reptile is a vertebrate. A mammal is an animal. An example in Unity that you
may have come across before is a Capsule Collider is a Collider. This concept will be covered further in the lesson on
polymorphism linked below. The idea of inheritance can be very useful and applicable in game development. We might, for example, have
a class called humanoid. This class covers all of the things that humanoids should do in our game. We then have two child
classes, enemy and player. These control the
specifics have how players and enemies work in the
game while already behaving like humanoids, because they’ve inherited all of the humanoid class’s members. We could then have two more child classes of enemy, orc and goblin. These already behave like enemies which in turn behave like humanoids. In this way, we have
much less code to write to make orcs and goblins behave as we want them to, because we’re reusing the code from humanoid and enemy. Constructors are an exception to what is inherited by child classes, as they remain unique to a class and are never shared. When a constructor is called
in a child class, however, the constructor of its parent class is called immediately before. Since classes can have many
different constructors, we might want to be able to control which base class
constructor is being called. We do this with the keyword base. By following the parameter list of the child’s constructor with a colon, you can explicitly call
a specific constructor of the base class using the keyword base in the base constructor’s parameter list. If the base class’s constructor
is not called explicitly, then the default constructor will still be called implicitly. Aside from calling the
base class’s constructor, you can also use the base keyword to access other members of the base class. This is useful for situations where you wish to access the base
class’s version of something, because it is different
than the derived version. This happens often when
overwriting functions. For more information on this, see the lesson on overriding linked below.

9 thoughts on “C# Inheritance in Unity! – Intermediate Scripting Tutorial

  1. Intermediate?……….

    So on a scale from 1 to 1,000,000 where every number describes a level of programming knowledge, intermediate starts somewhere around 6?

  2. I have an unrelated but still under the subject question:
    I've watched a whole playlist of these videos a couple of years ago, so, why upload it again with a different thumbnail?

  3. It's probably a good description but no examples or applications. Why should I do this? Instead of attaching the base class of my script to my gameobjects for example

Leave a Reply

Your email address will not be published. Required fields are marked *