C# Cheat Sheet
C# Cheat Sheet
Classes categorize variables and functions, similarly to how you might organise pictures in categorised
folders.
Variables and function in classes are placed within the classes brackets:
public class Modifiers
{
public bool leftAlt;
}
If they do not have a class, they are declared within the class of the script itself (see below)
Syntax:
Time.deltaTime
|
|
Class Name of variable in that class
Class of the script:
The name of the script must match the name of the class stated at the start of a new script (new scripts
are automatically called NewBehaviourScript). When the name of the script is changed, the class name
changes automatically. Shown below:
public class NewBehaviourScript : MonoBehaviour {
}
Everything in the script MUST take place within the brackets placed after the class of the script and
before the end of the script.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Variables
Variables are used for storing information that can be shared between scripts and parts of scripts.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Functions
Functions (also called methods) are used for carrying out actions and making things happen in general
1. Dog.fetchStick();
|
Function class and function name:
The name of the function (after .) and class it belongs in (before .)
2. Dog.fetchStick(25);
|
Argument:
Modifies the function (for example, fetch the stick with the speed 25.) Variables can also be placed here.
3. void Update();
|
Return type:
The type of data the function will result in:
void= Function will not receive data, only send it (most common)
4. Brackets:
Dog.fetchStick(){
}
Place whatever action the function carries out between the two brackets. For example:
void Update() {
float translation = Time.deltaTime * 10;
transform.Translate(0, 0, translation);
}
5. Basic functions:
Start ():Is called (activated) when scene first launches, for example, to set a score variable to
0 when the game begins.
Update (): Is called every frame. Crucial for checking for various variables and conditions in
game that need to be constantly checked upon.
Instantiate (): Spawns a prefab. Requires 3 pieces of information in parentheses, (what to make,
where to make it, rotation to give it)
if () {}: Checks for condition inside parentheses to be true, if it is, will carry out functions in brackets.
Rigidbody.AddForce () Applies force to object, expresed in parentheses as Vector3.
Input.GetAxis (): Returns value of axis specified in parentheses
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Comments
Comments are useful for writing down what a part of a script does, as the script will ignore
everything denoted as a comment but you will still be able to see it.
Think of annotation in Ruggis class, when you write an annotation it doesnt affect the actual
text, but you still see it and it tells you useful information.
Use // to denote a single line comment.
Start with /* to start a multi-line comment, end it with */
For example: