Conditions in Unity are a way to make objects react to events, check states, and make decisions in your code. For example, a character can jump only when standing on the ground, an enemy can attack if the player is nearby, and a door can open when a button is pressed.
To check conditions, we often use
comparison operators (like ==
,
!=
, >
) and
logical operators (such as &&
and
||
). We already saw them in the previous lesson β now
theyβll help us decide what to do depending on the situation.
In Unity (and in C# in general), the if
statement lets
you run a piece of code only when a certain condition is true. The
else
block runs if that condition is not met.
Inside the round brackets after if
, we write a
condition. This is an expression that gets evaluated
and returns either true or false. If
itβs true, the code inside the curly braces runs. If itβs false, that
part is skipped β or the else
runs if itβs provided.
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
int health = 10;
// Start is called once when the object is initialized
void Start()
{
}
// Update is called once per frame
void Update()
{
if (health > 0)
{
// Player is still alive
Debug.Log("Get up and fight!");
}
else
{
// Player is dead
Debug.Log("Game Over");
}
}
}
Here we check if the health is greater than zero. If it is, we print a message saying the player is still in the fight. If not, itβs time to restart the level. This is how most logic in games works.
In Unity, it's very common to check
boolean variables β such as isJumping
,
isDead
, isGrounded
. These are convenient
because they clearly show whether something is happening or not.
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
bool isDead = true;
// Start is called once when the object is initialized
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isDead)
{
// Character is dead
Debug.Log("Game Over");
}
}
}
You can also nest conditions inside each other for multi-level checks. This is useful when an action depends on several things at once β for example, the character can only jump if they are on the ground and the jump key is pressed. Though in such cases, it might be simpler to use logical operators to combine everything into one line.
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
bool isGrounded = true;
bool jumpKeyPressed = true;
// Start is called once when the object is initialized
void Start()
{
}
// Update is called once per frame
void Update()
{
// Nested conditions
if (isGrounded)
{
if (jumpKeyPressed)
{
Debug.Log("Jump performed (nested if)");
}
}
// Same result using a logical operator
if (isGrounded && jumpKeyPressed)
{
Debug.Log("Jump performed (logical AND)");
}
}
}
When you have many possible values and want to choose an action
depending on them, the switch
statement is very
convenient. It's like a menu: if one option is selected β do this, if
another β do that.
Inside the round brackets after switch
, we place a
variable or expression whose value we want to check. Then come the
case
s β possible values for that expression. If one of
them matches, the code inside that block runs. To stop the program
from continuing to the next block, we write break;
at the
end of each case. If no cases match, the default
block is
executed.
using UnityEngine;
public class CSharpScript : MonoBehaviour
{
string enemyType = "zombie";
// Start is called once when the object is initialized
void Start()
{
switch (enemyType)
{
case "zombie":
Debug.Log("Zombie attacks!");
break;
case "robot":
Debug.Log("Robot shoots!");
break;
case "ghost":
Debug.Log("Ghost vanishes!");
break;
default:
Debug.Log("Unknown enemy...");
break;
}
}
// Update is called once per frame
void Update()
{
}
}
π‘The break;
command ends the current case. If you forget
it, execution will continue into the next blocks, even if they donβt
match. This is called fall-through. Sometimes itβs done on
purpose, but more often itβs a mistake β and leads to unexpected
results.