FIZIX Labs

Unity Scripting Lesson 3: Beginning C# Scripting

  • Unity Scripting Lesson 3: Beginning C# Scripting

    Unity Scripting Lesson 3: Beginning C# Scripting

    Posted by FIZIX Richard on Fri 6th July 2012 1:01pm

    In this tutorial for Unity 3D we will be looking at the C# Scripting language, which is generally considered the option for serious or professional developers. For larger or more complex projects C# delivers many benefits such as having a stricter approach which reduces the potential for problems down the line and the performance gains you get from using C#.

    For example, while C# is a more involved and difficult language compared to UnityScript, the latter is a lot easier to create inefficient code with and introduce bugs and problems down the line due to casting issues.

    Within this tutorial we will begin by explaining the benefits of using C# and then going over how to create C# scripts, their structure and their syntax. This tutorial is aimed at both beginners and more seasoned developers who are transitioning from UnityScript to C#. For beginners it is assumed that you already have a general scripting knowledge so you know what we are talking about when we mention variables, functions, methods, classes etc. We are also assuming that you at least have a basic understanding of the Unity interface and general concepts such as game objects etc.


    In this tutorial we will be covering the following topics:

    1. Introduction to C#
    2. Creating a class
    3. Variables and properties
    4. Class methods
    5. Selection and iteration
    6. Interacting & communicating between game objects / classes


    Benefits of C#


    There are quite a few benefits to using C#, we won't list every single one of them but we will cover the most notable benefits.

    1. Strict Typing
    Variables have to be declared with a datatype using C#, so if you create a variable called health which will contain the characters health it will have to be given a datatype (for example an integer for a whole number). If you declare a variable as an integer you will not be able to insert anything other than integer numbers into the variable.

    This also translates to methods (functions), when you declare a method you will need to specify the datatype of any variables that are being passed to it along with the datatype for any data that is returned from it.

    Experienced developers will see an immediate benefit here. Games are complicated animals, more complex than most applications and definitely a lot more complex than websites (where loose languages like JavaScript and PHP are frequently used). One of the pitfalls of using UnityScript; a loosely defined language, is that you can introduce problems down the line resultant of casting issues and mistakes. It can also lead to code that is more difficult to debug.


    2. It is Object Orientated
    C# is an object orientated language, so scripts automatically have an OO structure, they are clearly defined and follow convention. While this may make them more complicated, particularly for beginners or those who have avoided OOP in the past, it should be remembered that games development was one of the major proponents that lead to OO concepts being designed and adopted. Also OO concepts fit games particularly well.


    3. Private by default
    All methods and variables are private by default, unless you specify otherwise. This is in contrast to UnityScript where all variables are public unless specified otherwise; while public by default makes life easier for the programmer as they do not have to worry as much about the scope and access permissions of their variables, this benefit is in itself a drawback as it can introduce problems down the line.

    Therefore C#'s approach of private by default leads to code that is stricter and less prone to issues and human error.


    4. Advanced features and .NET Framework support
    UnityScript is a custom language built for Unity, based on JavaScript, C# however is "true C#", meaning you gain access to the advanced features that C# has to offer along with full access to the .NET Framework. This also means that you are not restricted to functions that Unity supports and can go beyond Unity.


    5. Structure
    C# code while more complicated than UnityScript is structurally more organized, due to points 1, 2 and 3 it is more defined and to a professional programmer, clearer. There are also smaller nuances such as class names having to match the name of the script.



    There are other benefits, but these are the most cited and most impacting as together they lead to a language that is structured, organized, efficient and powerful (due to the .NET framework).


    Continue to part 2 where we will be looking at the structure of a C# Script.