Visual Scripting to C#

C# examples of the Visual Scripts covered in A Simple Puzzle Game.


Writing C# code may seem like a daunting endeavour. However, if you are coming from visual scripting, you will hopefully soon realize that much of the knowledge you have acquired so far is applicable in C#.

The structure of the content

Each page reflects the examples covered in A Simple Puzzle Game. The focus will be on the code and I encourage you to look at the corresponding example for more information about how the scene is set up.

The examples in A Simple Puzzle Game are designed to be followed in sequence and may at times build on each other.

A very brief explanation of how code becomes a program/app

As you may be aware, computers only process bits, 0s and 1s. But providing the computer with bits is not exactly a friendly way for humans to work with computers. Therefore, some humans decided to make a special kind of program known as a compiler that can translate human-readable code into something the computer understands.

When you write a script in C# in your Unity project, Unity automatically compiles your scripts (that is when you see the small spinning thingy in the lower right corner). If the compiler succeeds it will end without any fuss. However, if it reads some line of code that it does not know how to translate, it will scream out an error message. We will look at some examples later to help decipher these very technical messages.

While Unity provides a semi-baked program in the editor, it is not until we make our build that we get a program, game, or app we can share with the world. The build is the final compilation of our program where everything (including graphics and sound) is bundled together and can be referenced by our program.

Code syntax

Learning to read and write code is in many ways similar to learning something like musical notation or learning about the alphabet and how commas and periods structures words into sentences. The particular style and formatting requirements in a programming language like C# is what is known as its syntax.

Take a look at the following piece of C# code:

1
int moonDistance = 384400;

In another programming language, for example Python, it may look like this:

1
moonDistance = 384400

Both pieces of code achieve the same thing! They store some data in the computer’s memory that can accessed later using the name moonDistance. In this case the data is simply a number. You may be sad that we cannot simply use Python since it appears simpler, however, each kind of syntax comes with its own benefits

I will not go into more detail for now, but instead explain things as they become relevant.

I hope the information presented here will help you along in your coding adventure 🧗‍♀️


The starter project can be downloaded from here: Visual Scripting to C-Sharp - Starter Pack.zip


The project uses emojis designed by OpenMoji – the open-source emoji and icon project. License: CC BY-SA 4.0


Table of contents