Mastering Unity Scripting | Summary — Chapter 1: Unity C# Refresher

Ayşe Oduncu
4 min readDec 9, 2021

--

In this chapter, we will see the general C# and C# in Unity briefly. That chapter considers that you have some prior knowledge of C# and Unity.

(Note: The code extensions below are .java because of the colouring and easy-reading of the code (somehow GitHub doesn’t colour the codes right in .cs format that is the C# file format), however, codes are all same in C#.)

Variables

Variable data types:

  • int: For integers like 1, -1, 0, 3, …
  • float: For floating point numbers like -0.4, -0.97, …
  • bool: For booleans, either true or false
  • Vector3: For 3 dimensional vectors like a position value (0, 0, 0)…
  • etc.

Conditional Statements

  • “if” and “if-else” statements: The basic condition checking, if the condition is true, the code under “if” works, if, else if the condition is true, then the code under “else if” works, else (meaning that none of the above is true), the code under “else” works.
  • “switch” statement: It lets you check a variable for multiple possible directions and also has a default for the condition of none of them being true.

Example from book:

Arrays

One type of list. Each item in the array is a unit of information that has the potential to change during gameplay, and so a variable is suitable to store each item.

Example from book:

Loops

  • foreach Loop: Using foreach, you can cycle through every element in an array, sequentially from start to end, processing each item as required.
  • for Loop: Has more control over processing elements, takes a start and an end number and processes through code.
  • while Loop: While a condition is true, processes through the code. (can turn to an infinite loop and causes problems or can be handled and used for special cases such as moving platforms)

Functions

A function is a collection of statements bundled together as a single identifier block, which is given a collective name and can be executed in demand, each line of the function being executed in sequence.

Events

Events are functions called to notify an object that something significant has happened; level has begun, an enemy has died, etc.

Classes and Object-Oriented Programming

A class is an amalgam of many related variables and functions, all brought together into a self-contained unit.

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).” (Wikipedia)

Classes and Inheritance

Inheritance allows you to create a completely new class that implicitly absorbs or contains the functionality of another class, that is, it allows you to build a new class that extends an existing class without affecting the original one. When inheritance happens, two classes are brought into a relationship with each other. The original class is known as the case class or ancestor class. The new class, which extends to the ancestor class, is called a superclass or derived class.

Classes and Polymorphism

The word polymorphism means having many forms. In the object-oriented programming paradigm, polymorphism is often expressed as ‘one interface, multiple functions. More at:

Commenting

By putting three forward slashes on top of a function (///), MonoDevelop will automatically insert an XML comment ready for you to complete with appropriate descriptions. Then, when using that function, the code-completion pop-up helper will display both the summary comment function as well as the parameter comment’s context sensitively.

Example from book:

Commenting example from the book

Variable Visibility

By using the attribute [SerializeField] before a private variable, you can make it visible in the inspector.

SendMessage and BroadcastMessage

SendMessage invokes a specified function across all components attached to a single GameObject. BroadcastMessage incorporates the SendMessage behaviour and goes a stage further, that is, it invokes a specified function for all components on GameObject and then repeats this process recursively for all child objects in the scene hierarchy, cascading downwards to all children.

Additional Notes

  • Enumeration (enum): A special structure used to store a range of potential values for one or more variables.
  • ToString: Converts an object to a string.
  • By using the “?” operator, you can write short if statements. Example from book:

End of Chapter 1: C# Refresher

Chapter 2: Debugging →

--

--

Ayşe Oduncu

I am currently a computer engineering student at Muğla Sıtkı Koçman University. I love many things about game development, so, I will share stories about them.