Get Started. It's Free
or sign up with your email address
Rocket clouds
C# by Mind Map: C#

1. Variables

1.1. type nameOfVariable; type nameOfVariable = value;

1.2. Integer

1.2.1. float

1.2.1.1. The float data type stores floating point numbers (numbers with decimal places). The float data type is the default number type in Unity eg. 5.25

1.2.2. int

1.2.2.1. The integer data type stores positive or negative whole numbers eg. 5

1.2.3. double

1.2.3.1. The double data type stores floating point numbers but can hold larger size numbers than floats. It is not the default number type in Unity though.

1.3. Bool

1.3.1. bool

1.3.1.1. The bool (short for Boolean) data type stores true or false values only eg. true

1.4. String

1.4.1. string

1.4.1.1. The string data type stores letters, numbers and other characters in the form of words or sentences. A string value is written inside double quotes eg. “Hello World”.

1.4.2. char

1.4.2.1. The char data type stores a single character such as a letter, number, space or special character (eg. a, 1, !). A char value is written in single quotes eg. ‘a’.

2. Functions

2.1. type FunctionName(type parameterName); { }

2.2. MonoBehavior Functions

2.2.1. OnEnable

2.2.1.1. Implement OnDisable and OnEnable script functions. These functions will be called when the attached GameObject is toggled.

2.2.2. OnDisable

2.2.3. Awake

2.2.3.1. References between scripts, initialization. Activated even if the scripts is not enabled ex: Set ammo for the Enemy

2.2.4. Start

2.2.4.1. Once the script is enabled the Start function will work.

2.2.5. Update

2.2.5.1. It is called every frame (or second when the function Time.deltaTime is used) Used for regular updates such as: -Moving non-physics objects -Simple Timers -Receiving outputs Update Interval times vary

2.2.6. FixedUpdate

2.2.6.1. Called every Physics Step Fixed update interval are consistent Used for regular updates such as: -Adjusting Physics (Rigidbody) Objects

3. Syntax and Conventions

3.1. (;) semicolon

3.2. (.) dot operator

3.3. It is used to terminate statements

3.4. (//) comment

3.4.1. It allows you to separate or access different different elements within a compound Item in Unity

3.4.2. It is used to add a line in your code that will not be executed. It is useful if you want to explain a line of code

3.4.3. (/* and */)

3.4.3.1. It is used to add comment in 2 different line

4. If Statements

4.1. if

4.1.1. When we run the program, the output will be:

4.1.2. 2 is less than 5

4.1.3. This statement is always executed.

4.1.4. The value of number is initialized to 2. So the expression number < 5 is evaluated to true. Hence, the code inside the if block are executed. The code after the if statement will always be executed irrespective to the expression.

4.1.5. Now, change the value of number to something greater than 5, say 10. When we run the program the output will be:

4.1.6. This statement is always executed.

4.1.7. The expression number < 5 will return false, hence the code inside if block won't be executed.

4.2. else if

4.2.1. The if...else if statement is executed from the top to bottom. As soon as a test expression is true, the code inside of that if ( or else if ) block is executed. Then the control jumps out of the if...else if block. If none of the expression is true, the code inside the else block is executed.

4.2.1.1. using System; namespace Conditional { class IfElseIfStatement { public static void Main(string[] args) { int number = 12; if (number < 5) { Console.WriteLine("{0} is less than 5", number); } else if (number > 5) { Console.WriteLine("{0} is greater than 5", number); } else { Console.WriteLine("{0} is equal to 5"); } } } }

4.2.1.1.1. When we run the program, the output will be: 12 is greater than 5 The value of number is initialized to 12. The first test expression number < 5 is false, so the control will move to the else if block. The test expression number > 5 is true hence the block of code inside else if will be executed. Similarly, we can change the value of number to alter the flow of execution.

4.3. else

4.3.1. using System; namespace Conditional { class IfElseStatement { public static void Main(string[] args) { int number = 12; if (number < 5) { Console.WriteLine("{0} is less than 5", number); } else { Console.WriteLine("{0} is greater than or equal to 5", number); } Console.WriteLine("This statement is always executed."); } } }

4.3.1.1. When we run the program, the output will be: 12 is greater than or equal to 5 This statement is always executed. Here, the value of number is initialized to 12. So the expression number < 5 is evaluated to false. Hence, the code inside the else block are executed. The code after the if..else statement will always be executed irrespective to the expression. Now, change the value of number to something less than 5, say 2. When we run the program the output will be: 2 is less than 5 This statement is always executed. The expression number < 5 will return true, hence the code inside if block will be executed.

4.4. nested if statements

4.4.1. The general structure of nested if…else statement is: if (boolean-expression) { if (nested-expression-1) { // code to be executed } else { // code to be executed } } else { if (nested-expression-2) { // code to be executed } else { // code to be executed } }

4.4.1.1. Nested if statements are generally used when we have to test one condition followed by another. In a nested if statement, if the outer if statement returns true, it enters the body to check the inner if statement.

4.4.1.1.1. using System; namespace Conditional { class Nested { public static void Main(string[] args) { int first = 7, second = -23, third = 13; if (first > second) { if (firstNumber > third) { Console.WriteLine("{0} is the largest", first); } else { Console.WriteLine("{0} is the largest", third); } } else { if (second > third) { Console.WriteLine("{0} is the largest", second); } else { Console.WriteLine("{0} is the largest", third); } } } } }