Main

C# Overriding in Unity! - Intermediate Scripting Tutorial

Watch this video in context on Unity Learn: https://learn.unity.com/tutorial/overriding Overriding allows you to replace the members of a parent class with newer members from the child class. This allows child classes which have been Upcasted to a parent class to be treating like a parent class while behaving like a child class. In this video you will learn how to implement overriding in a parent / child class relationship.

Unity

4 years ago

Overriding is the practice of changing a parent class method in a child class. The result is that when we call the method, the newest, or most overridden version of the method is called. When working with inheritance hierarchies, we often want to use slightly different versions of functions from base classes. Doing so is easy. You simply have to recreate the method in the child class and write it to do whatever it needs to do. Consider a situation where we have a Humanoid base class, which has a
n Enemy derived class, which in turn has a Orc derived class. The Humanoid class has a function called Yell(). When Yell() is called by a Humanoid the model makes a yelling noise, and puts its hands up to cup around its mouth. The Yell() method is automatically inherited by the Enemy class. However, we want to modify it. We want the enemy to attract other enemies. We also want Orc to be able to call this function. But when Yell() is called from an Orc object, all of the Orcs in the area get a bo
nus to their attacks for a short time. What we effectively need to do is override the parent version of the Yell() method in each of the child classes. Unity will warn us when we attempt to override a parent method in a child class. In order to suppress that warning, and tell Unity that we meant to override the method, we use the "virtual" and "override" keywords. These keywords go before the return type of the method. The method in the parent class is defined as "virtual", and the methods in al
l of the children classes are defined as "override". Any method that is declared virtual can be overridden by any child class. A more interesting use of overriding would be if you wanted each child class to add specific functionality to a method, without losing the original functionality provided by the parent. In order to do this, you need to use the base keyword to call the parent version of the method as well. In our previous example, let's say we wanted Enemy to keep the functionality of Hum
anoid while adding its own effect. We also want Orc to keep the functionality of Enemy while adding its own effect. To accomplish this, we would need to put a base call to the parent Yell() method in both Enemy and Orc. Now, when the Orc Yell() method is called, it will call Enemy's Yell() method, which in turn will call Humanoid's Yell() method. Overriding is also very useful with polymorphism. By declaring the parent method as virtual, and the child method as override, we will effectively over
ride the parent version of the method. The result is that when we upcast a child reference into a parent object, and then call a method, the child version of that method will be called.

Comments