C Sharp Tutorial

Quick information about using C# in Unity.

Every Unity C# starts with a list of Using .... These are library lists of functions that Unity provides for you to use. The main ones are using UnityEngine;

using UnityEngine.UI;

using System.Collections;

using System.Collections.Generic;

using System.Net.Mime;

using Unity.Mathematics; If you include those you will be set for most beginner code.

Then it has a class declaration which has to match your file name and asset name in Unity public class Stats : MonoBehaviour The next section starts with {

and then your variable declarations.

Each variable declaration has generally one of the prefixes

Any one of these can be converted to an array (1D) with [] after the type

Lesson 1: Variables
In the C#, when you declare the variable you have to write the type of it.

Variables are the areas in Operating Memory to which we have access.

The Example: int x; The basic types of variables: In the brackets I wrote the keywords which we put in code.
 * Integers (int)
 * Floats (float)
 * Strings (string)
 * Characters (char)

To set the value of variable you should use = operator. Example: x = 10;

Remember that almost every line in C# ends with ; About the exceptions we're gonna talk later.

To increase variable's value by 5 for example you can do this like this: x = x + 5; or x += 5; To increase the value by one you can also write this like this: x++;

For decreasing value you just replace in code above + for -. Example: x--; For multiplication you can only use first and second way. Just replace + for *. Using x** would not work for obvious reasons. Division is analogous for multiplication just instead of * you write /

Example of C# program which you can run in Unity: Example.

[[Category:C]]