Object-Oriented Programming (OOP) — Unity C#

Imran Momin
4 min readApr 25, 2021

If class and struct are the blueprints of our program, then OOP is the architecture that holds everything together. It has specific principles for how the overall program should work and communicate. Essentially, OOP focuses on objects rather than pure sequential logic — the data they hold, how they drive action, and, most importantly, how they communicate with each other.

Things in the physical world operate on a similar level; when you want to buy a soft drink, you grab a can of soda, not the liquid itself. The can is an object, grouping related information and actions together in a self-contained package. However, there are rules dealing with objects, both in programming and the grocery store.

For instance, who can access them — different variations and generic actions all play into the objects all around us. In programming terms, these rules are the main tenants of OOP: encapsulation, inheritance, and polymorphism.

Encapsulation

One of the best things about OOP is that it supports encapsulation — defining how accessible an object’s variables and methods are to outside code.

Take our soda can as an example — in a vending machine, the possible interactions are limited. Since the machine is locked, not anyone can come up and grab one; if you happen to have the right change, you will be allowed provisional access to it, but in a specified quantity. If the machine itself is locked inside a room, only someone with the door key will even know the soda can exists.

Adding a reset — Example

Our Character class is public, as are its fields and method. However, what if we wanted a method that can reset a character’s data back to its initial values? This could come in handy but can prove disastrous if it was accidentally called, making it a perfect candidate for a private object member.

  1. Create a private method, called Reset, with no return value inside the Character class. (Set the name and exp variables to “Not assigned” and “0” respectively.

2. Try and call Reset from the LearningCurve script.

Marking a method or variable as private will make it inaccessible using dot notation.

Inheritance

Just as in life, a C# class can be created in the image of another class, sharing its member variables and methods but able to define its unique data. In OOP, we refer to this as inheritance, and it’s a powerful way of creating related classes without having to repeat code. Take the soda example again — there are generic sodas on the market that have all the same basic properties, and then there are special sodas. The special sodas share the same basic properties but have different branding, or packaging, that sets them apart. When you look at both side by side, it’s obvious that they are both cans of soda — -but they are also obviously not the same.

Thy original class is usually called the base or parent class, while the inheriting class is called the derived or child class. Any base class members marked with the public, protected, or internal access modifiers are automatically part of the derived class — except for constructors. Class constructors always belong to their containing class, but they can be used from derived classes to keep repeated code to a minimum.

Composition

Aside from inheritance, classes can be composed of other classes. There will be times when you want to make new objects out of a combination of other existing objects.

Think of legos:

You don’t start building from nothing — you already have blocks of different colors and structures to work with. This is called Composition.

Polymorphism

Polymorphism is the Greek word for many — shaped and applies to OOP in two distinct ways:

  1. Derived class objects are treated the same as parent class objects.
  2. Parent classes can mark methods as virtual, meaning that their instructions can be modified by derived classes using the override keyword.

Method in Parent class:

public virtual void MethodName()
{

}

Method in derived class:

public override void MethodName()
{

}

OOP roundup

· OOP is all about grouping related data and actions into objects — objects that can communicate and act independently from each other.

· Access to class members can be set using access modifiers, just like variables.

· Classes can inherit from other classes, creating trickle-down hierarchies of parent/child relationships.

· Classes can have members of other class or struct types.

· Classes can override any parent methods marked as virtual, allowing them to perform custom actions while retaining the same blueprint.

--

--

Imran Momin

A VR/AR developer, who enjoys making games and developing interactive environments using Unity’s XR integration toolkit for Oculus quest and HTC vive devices.