Please share...

  • Video courses

    All game programming videos just $19 - For a very limited time these three videos and thousands more are up to 90% off!

    Just enter coupon code NEWU19 in cart/checkout

C# for Beginners: Learn Fundamentals by Coding

Highly acclaimed course: Over 13,000 students

C# Intermediate: Classes, Interfaces, and OOP

In-depth, classes, interfaces, and OOP.

Learn To Code By Making a 2D Platformer in Unity

Beginning Unity & C# with a real game

Object Oriented Programming, or OOP for short, is the way almost all programming is done, in almost all languages. It hasn’t been this way forever and there are some exceptions but if you want to make games, it’s a topic that needs to be learnt. OOP is designed to help us write code for games that are more complex than we could possibly manage, without OOP.

About this tutorial

Skill level 1
Time to read: 10 minutes

New Concepts:

  1. Classes
  2. Objects
  3. instances of classes
  4. Inheritence
  5. Polymorphism
  6. Encapsulation

Projects related to or that demonstrate these concepts

OOP involves breaking our game’s requirements into pieces that are more manageable and logical than the whole. Each piece is self contained and yet potentially usable in more than one game! These pieces of code are called objects. When we plan an object, we do so with a class. A class can be thought of as the blueprint for the object.

We implement an object of a class. This is called an instance. Think about a house blueprint? You can’t live in it, but you can build a house from it, you build an instance of it. However, OOP is more than this. It is also an entire paradigm.

The three fundamentals of OOP

Encapsulation: This means keeping the internal workings of your code safe from interference from the programs that use it. Allowing only the variables and methods you choose to be accessed. This means your code can always be updated, extended or improved without affecting the programs that use it, as long as the exposed parts are still accessed in the same way.

Inheritance: Just like it sounds, inheritance means we can harness all the features and benefits of other peoples’ classes, including encapsulation and polymorphism, while refining their code specifically to our situation. We will do this in every single Unity game project we make.

Polymorphism: This allows us to write code that is less dependent on the types we are trying to manipulate, making our code clearer and more efficient.

C# was designed from the start with all of this OOP stuff in mind.

We will use loads of other people’s classes (mainly Unity classes) when we make our games- it is really easy! However, we also need to make our own classes (zombies, shotguns, levels etc) so for the rest of this tutorial we will concentrate on how we do that.

Building our own classes

A class is some code that can contain methods, variables, loops and any other C# code we have learnt about in these tutorials. Each new class will be defined in its own code file with the same name as the class. You might have noticed the following code near the top of the MonoDevelop code editor during the Unity & C# part 1 or part 2 projects.

public class NewBehaviourScript...

Once we have written a class we can use it to make as many objects from it as we need. Remember, the class is the blueprint and we make objects based on the blueprint. Here is a hypothetical example of a Tank class in C#.

class Tank {

    public int armour;
    public int shellsRemaining;

    void shootEnemy(){

        // bang

    }

}

The previous code is an implementation for a class called Tank. Within the Tank class are two variables, an int variable called armour and a int variable called shellsRemaining. The public keyword is the C# access specifier that means the variable can be directly accessed from any objects we make from the class.

There is also a method, shootEnemy. The method has no parameters and a void return type but class methods can be of any shape or size that we discussed in the tutorial Structuring C# games with methods. Remember this is just a class not an object. It is a blueprint for a tank not an actual tank. This is how we make an object of type Tank from our Tank class.

Tank myTank = new Tank();

In the first part of the code, Tank myTank declares a new object variable of type Tank called myTank The last part of the code new Tank() creates an actual Tank object. And of course the assignment operator = in the middle of the two parts assigns the result of the second part to that of the first. Just like regular variables.

This is how we would assign to and use the variables inside the Tank class. Variables which are part of a class and not contained inside a method are called members.

myTank.armour = 100;
myTank.shellsRemaining = 20;

// Notice that we use the object name myTank.
// Not the class name Tank.
// We didn't do this:
// Tank.armour = 100; ERROR!

Above, the dot operator “ .” is used to access the variables of the class. This is also how we use the method, by using the object’s name followed by the dot operator.

myTank.shootEnemy();

We can make as many Tank objects as we like.

Tank myTank2 = new Tank();
myTank2.armour = 25;
myTank2.shellsRemaining = 10;
myTank2.shootEnemy();

// Add as many tanks as you like

It is important to realize that myTank2 is a totally separate object with its very own variables.

Access modifiers

All of our class’ variables, methods and the classes themselves can be given different access modifiers. In the previous example we saw the public access modifier. We use access modifiers to specify which other classes can access different parts (variables, methods, classes) of our code. This topic on its own is a fairly enormous one but we don’t have to master it to start making games. When we make and use classes during our Unity game projects I will point out when and why we use a particular access modifier.

The two main access modifiers are public and private. Just know that public methods and variables are almost completely accessible and private are generally hidden within the class itself. This allows us to write code that uses variables that can only be manipulated by the code within the class itself. For example we wouldn’t want a hypothetical Marshmallow class altering our tanks armour variable!

Did you follow all that?

It doesn’t really matter. As long as you understand that we can design a class which acts like a code blueprint and that we can create objects from that blueprint that actually use that code, you know enough to proceed. I will fully explain what is happening when we design some classes and create objects from them in our Unity game projects. Note that OOP is an enormous topic that can fill entire books. I just think it is more fun to learn about it while making games, rather than becoming an OOP expert first.

The best way to reinforce your knowledge of C# classes would be to go and play around with some. In the project Unity 2d Controllable spaceship project where we will take a look at a few classes provided by Unity, make some objects from them and use them in our first project that actually starts to resemble a real game.