Introduction to C#
Swipe to show menu
Introduction to C#: Variables, Operators, and Core Concepts
Now that you've practiced pseudocode, it's time to learn the basics of C#, the language Unity uses.
Start() vs Update()
Start() -> runs once when the scene loads; use for setup like initializing variables or references.
print("hello"); // Prints once
Update() -> runs every frame; use for input, movement, and interactions.
print("hello"); // Prints continuously
Operators: How Programs Think
Programs are sets of logical instructions. C# uses operators to express logic.
Download the spreadsheet (linked below) for full explanations and examples.
Operator types with simple explanations:
- arithmetic operators -> perform calculations;
- relational operators -> compare values, return true or false;
- logical operators -> combine conditions;
- assignment operator -> sets a value to a variable.
Arithmetic operators example:
print(2 + 3); // Adds numbers, prints 5
print(10 / 2); // Divides numbers, prints 5
Relational operators example:
print(5 > 3); // true
print(2 == 4); // false
Logical operators example:
print(true && false); // false cause only one is true
print(true || false); // true cause at least one is true
Assignment operator example:
int score = 10; // assigns 10 to score
If Statements
Code often follows: if condition is true -> do something.
if (1 > 2)
print("yes");
else
print("no"); // Prints "no" instead of "yes".
Every statement ends with a semicolon (;).
Variables: Storing Information
Variables store and change information like speed, position, score, text, or game states.
Declaration includes: type, name, optional starting value.
Usually declared above Start().
Common Variable Types
Download the spreadsheet (linked below, page 2) for full list with examples.
Public vs Private Variables
Private -> only accessible inside the script (default).
Public -> visible in Inspector; tweak during playtesting.
Debugging with the Console
Text must be inside quotes:
print("hello");
Forgetting quotes causes errors.
Download: Operators & Variables Spreadsheet
Happy coding!
1. What is the main difference between the Start() function and the Update() function in Unity?
2. You want to store how fast the player moves, and you may want to fine-tune this value using decimals (for example, 1.5). Which variable type is most appropriate?
3. You want to print the word hello to the Console in Unity. Which line of code is written correctly?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat