Notes for Noobs — Object-Oriented Basics for Unity3D


Classes are blueprints for your objects. Basically, all of your scripts will begin with a class declaration that contains something like this:

public class PlayerController : NetworkBehaviour

This tells Unity you’re creating a class with the name PlayerController. Ignore the ‘public’ and everything after the colon for a moment. More on that soon.

Objects and Instances

The whole point of Object-Oriented Programming is to allow the programmer to develop software in modules. This is accomplished through objects. Objects contain data, like integers or lists, and functions, which are usually called methods. You might think of the data contained in an object as its ‘traits’ and the methods as its ‘behaviors’. Objects are built from a class blueprint, which work like the object’s DNA.

For example,

PlayerController Player1 = new PlayerController();

creates an object named Player1 based on the blueprint (class) PlayerController.

These objects are instances of the class. In Unity’s gameObject system, you’ll see the method “instantiate” called many times. This creates an instance (object) of a class.


Constructors are used to customize objects that are created by a class. For example, you could have the class “Stone”. Stones come in different sizes, and class Stone could have a variable within it for size. A constructor shares the same name as a class. So you might see:

public class Stone : NetworkBehaviour {
int Weight
Stone (initialWeight) {
weight = initialWeight

This would allow you to customize each instance of the Stone object by passing a value to the object. For example,

Stone Stone1 = new Stone(50);

Creates a new object called Stone1 from the Stone class with an initialWeight of 50, which becomes the stones ‘weight’.


In Unity, encapsulation occurs through the use of the terms ‘public’ and ‘private’. Elements termed ‘public’ are accessible from outside the object. Elements termed ‘private’ are encapsulated within the object — only the object can access them.


Inheritance is precisely as it sounds. Objects that have similar traits but are still different can be nested within classes. For example, our declaration above

public class PlayerController : NetworkBehaviour

indicates that the class is new and named PlayerController but that it inherits the features of the class NetworkBehaviour.


Methods, or functions, that are called in a script will be applies to the object they are attached to. This means that a function that operates on a particular Component will only work if the object has a game component of that type. Remember to ‘call’ the function somewhere, or the script won’t run.


[Command] is called by the client but executed by the server. It may only be sent by the local Player object (a unique type of object and prefab in the Unity world).

[ClientRpc] is called by the server and executed by the client.

Spawning is handled uniquely by Unity networking. An object is not instantiated when it is spawned. The object is created on the server and all clients, but it is managed by the spawning system itself.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.