Operations in C# are the way we work with data: adding points, comparing speed, checking whether the player is on the ground, and much more. Without them, scripts would just be collections of variables without any behavior.
It's operations that make code come alive: they allow it to respond to events, change object parameters, and make decisions. They're used everywhere β from simple counting to complex behavior logic.
In this section, weβll go over the main types of operations youβll most often encounter in Unity. Everything will be simple, with examples and explanations of why they matter.
Operator | What it does |
---|---|
= |
Assigns a value to a variable |
+= |
Adds a value to a variable and stores the result |
-= |
Subtracts a value from a variable and stores the result |
*= |
Multiplies a variable and stores the result |
/= |
Divides a variable and stores the result |
%= |
Stores the remainder after division |
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
int health = 10; // Character health
int damage = 1; // Damage dealt
float speed = 5f; // Character speed
string color; // Object color (not set yet)
int score = 0; // Player's score
// Start is called once when the object starts
void Start()
{
// Decrease health by damage (health = health - damage;)
health -= damage;
// Double the speed (speed = speed * 2;)
speed *= 2f;
// Add 10 to score (score = score + 10;)
score += 10;
// Divide speed by 2 (speed = speed / 2;)
speed /= 2f;
// Assign color
color = "red";
}
// Update is called every frame
void Update()
{
// Create a copy of the color
string firstColor = color;
// Subtract 3 from score (shorthand subtraction)
score -= 3;
// Multiply health by 2 (shorthand multiplication)
health *= 2;
}
}
Operator | What it does |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Remainder (modulo) |
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
int a = 10;
int b = 3;
float speed = 5.5f;
float time = 2f;
int remainder; // Variable to store remainder of division
int quotient; // Variable to store result of division
// Start is called once when the object starts
void Start()
{
int sum = a + b; // Addition: 10 + 3 = 13
int diff = a - b; // Subtraction: 10 - 3 = 7
int product = a * b; // Multiplication: 10 * 3 = 30
quotient = a / b; // Integer division: 10 / 3 = 3
remainder = a % b; // Modulo: 10 % 3 = 1
float speedPerSecond = speed * time; // Float multiplication: 5.5 * 2 = 11.0
float averageSpeed = speed / time; // Float division: 5.5 / 2 = 2.75
}
// Update is called every frame
void Update()
{
}
}
Operator | What it does |
---|---|
== |
Checks if values are equal |
!= |
Checks if values are not equal |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
int score = 10;
int highScore = 15;
float speed = 5f;
float maxSpeed = 5f;
bool isEqual;
bool isFaster;
// Start is called once when the object starts
void Start()
{
isEqual = score == highScore; // false β scores are not equal
isFaster = speed > maxSpeed; // false β not faster
bool slowedDown = speed < maxSpeed; // false β speed equals max
bool atLimit = speed >= maxSpeed; // true β speed is at or above max
}
// Update is called every frame
void Update()
{
}
}
π‘Comparison operators are most often used inside conditions β for example, to check whether the player has earned enough points or reached a goal. Weβll explore conditions in more detail in the next lesson.
Operator | What it does |
---|---|
&& |
AND β returns true only if both expressions are true |
|| |
OR β returns true if at least one of the expressions is true |
! |
NOT β logical negation (inverts true to false and vice versa) |
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
int a = 5;
int b = 10;
int c = 5;
bool resultAnd;
bool resultOr;
bool resultNot;
// Start is called once when the object starts
void Start()
{
// Check if a equals c β true
// and at the same time: is b greater than 5 β true
resultAnd = (a == c) && (b > 5); // true && true β true
// Check if a is less than 3 β false
// or b equals 10 β true
resultOr = (a < 3) || (b == 10); // false || true β true
// Negate the comparison result
resultNot = !(a > b); // !(false) β true
}
// Update is called every frame
void Update()
{
}
}
π‘ Be careful when working with variables of different types (for
example, int
and float
). Sometimes C#
automatically converts one type to another, but in some cases, this
can lead to unexpected results or even a compilation error. It's
better to make sure both values are of the same type β or convert them
explicitly.