Week 7 (part2): Intro to Coding in Unity

Understanding how to use C# as a design tool in Unity3D

Nissie Bungbrakearti
code3100
5 min readMay 3, 2017

--

After becoming more familiar with Unity and its functionalities it is obvious that coding is used to customise designs and interactive events. The coding language used is C# and lucky for me I found some Scripting in Unity tutorials on Lynda.com. These tutorials are great because you can watch at your own speed, they are often very informative, and a transcript is provided so you can read along or go back to a point that you wish to revisit. The tutorial I am going through can be found here.

Introduction to Code in Unity

It should be noted that every game object has unity scripts attached to it in the form of components which can be seen on the right hand side of the editor in the Inspector panel. Each component is a C# script that has been scripted to modify a certain element of that game object. For example, the camera game object which is always included as a default game object has the components Transform, camera, GUI Layer, Flare Layer and Audio Listener.

The console which can be found at the bottom displays messages from your code such as warning messages.

To view code within your project you can go to Assets in the top tool bar > Open C# Project, and this will open Visual Studio which enables you to edit the scripts and update them instantaneously.

Currently there are no C# scripts for us to edit and so the Visual Studio opens to an empty screen.

To create your own script you can right click inside the Assets Folder down the bottom. Right click > Create > C# script will create a new script in the selected folder. When selected the script can be seen in the right Inspectors panel, but not edited. To edit the script, it needs to be opened in Visual Studio.

To add a script to a game object, simply drag the script you want from the bottom panel onto the game object. The new script should then appear in the Inspector panel.

What is a Variable?

Adding values to the script requires us to create variables. Additionally, the variable type is determined by where it is located inside the script. There are different types of variables. For example:

STRING = any text you want to give a variable

BOOLEAN = a value that can either be true or false

FLOAT = a value that is a number with decimals

INTEGER = a value that is a whole number.

RETURN = lets us know if a method will give us a value back

Additionally, a field is “ is a variable that exists inside of a class outside of the scope of a method”.

In Visual Studio I have created a string and set the field to text whilst using “ “ as a way to identify that we are dealing with a string. As seen below, a red squiggly line will appear when a line is incomplete. To complete this text, a semi colon is used “ ; “ .

By adding “public” to the beginning of “string” we are able to view this variable publicly in Unity and change it without having to go back into Visual Studio. Having a variable be “private” means that it will not be visible within the Unity Inspector. To update the script in Unity simple press the save button outlined below in Visual Studio. Once the string is made public, we can see that it is visible in the Unity Inspector.

Now testing out the boolean variable. Using the public variable bool turnOn; and saving the code creates a true or false variable in our Unity Inspector. This check box allows for us to manually switch something to true or false.

Like the previous examples, I’ve then made a public float and integer variable where the float represents the X position and the integer is a test. Again, once saved in Visual Studio and compiled in Unity, it will update and appear in teh Inspector panel. Hovering your mouse over these new public variables will show arrows that act as a slider.

What are functions?

“A function is a way of wrapping code so we can re-use it whenever we want...Functions allow us to keep code isolated from the execution order when the script runs.”

Starting off with adding a function to the start method, in here I have stated that once played “text” will change to “Updated during Start()” instead of “hello code3100”. We can see that this is successful in the screenshot below. Once we exit play mode however, the text reverts back to “hello code3100” instead of “Updated during start()”. This means that every time we exit play mode, functions within void start method revert back to default values.

We can also create our own methods within the script. Below I have created a return type that will return us a value. Using void means that we don’t want anything returned. Below we can see it now says “Updated during text”. Using methods we can control the script and the way it runs. Unity calls start when the script loads up and Update when the game is played.

--

--