Featured Articles:

Friday, September 25, 2026

If And Switch Statements In C# Explained: Conditional Logic For Unity Devs

It's Friday!! And welcome to another blog post about a piece of logic you'll write more than almost anything else in your game: if statements in C#. If you've built even a tiny prototype in Unity, you've already written dozens of these without necessarily thinking about the "why" behind them: checking if the player's health hit zero, checking if a key was pressed, checking if an enemy is in range. 


Let's dive in!


What Is Conditional Logic, Really?

Conditional logic is just a fancy term for "do this, but only if that's true." Every game you've ever played is riddled with it. Is the player grounded? Then allow a jump. Is the inventory full? Then block the pickup. Is the boss's health at zero? Then trigger the death sequence.

An if statement in C# is how you write that logic down. It checks a condition, and if that condition evaluates to true, the code inside runs. If it's false, Unity skips right past it like it was never there.

check condition  →  true? run the code inside  →  false? skip it entirely

That's the entire mental model. Everything else in this article is just variations on that same idea.


The Basic if Statement

Here's the simplest possible example, checking whether a player's health has dropped to zero:

if (health <= 0)
{
    Debug.Log("Player has died.");
}

Break it down: the condition sits inside the parentheses, health <= 0, and the code that runs when that's true sits inside the curly braces. If health is still above zero, Unity never even looks at what's inside those braces. This exact pattern, an if else statement Unity developers write constantly, forms the backbone of almost every gameplay script you'll ever touch.




Adding else: Handling the Opposite Case

Most of the time you don't just want to handle the true case; you want to handle what happens when the condition is false too. That's where else comes in.

if (health <= 0)
{
    Debug.Log("Player has died.");
}
else
{
    Debug.Log("Player is still alive.");
}

Now you've covered both outcomes with a single, clean block. This is the classic if-else statement Unity pattern you'll see in nearly every tutorial, and for good reason: it reads almost like plain English once you're used to the syntax.


else if: Handling Multiple Conditions

Sometimes two outcomes aren't enough. Maybe you want different behavior depending on exactly how low health is, not just "alive" or "dead." That's what else if is for, chaining multiple conditions together in order.

if (health <= 0)
{
    Debug.Log("Player has died.");
}
else if (health < 25)
{
    Debug.Log("Health critical, screen should flash red.");
}
else if (health < 50)
{
    Debug.Log("Health low, play warning sound.");
}
else
{
    Debug.Log("Health is fine.");
}

Unity checks these top to bottom and stops at the first one that's true. So, if health is 15, it matches the "critical" branch and never even bothers checking the "low" branch below it, since the whole chain already found its match. Order matters here more than beginners usually expect; put your conditions in the wrong sequence and you'll get logic bugs that are genuinly annoying to track down.


Comparison and Logical Operators You'll Actually Use

Every condition in an if statement in C# boils down to some combination of these operators. Get comfortable with them, since you'll type them constantly:

  • == - equal to. Not to be confused with a single =, which assigns a value instead of comparing one. This mix-up trips up beginners constantly.
  • != - not equal to.
  • > and < - greater than and less than.
  • >= and <= - greater than or equal to, less than or equal to.
  • && - logical AND. Both sides need to be true for the whole thing to be true.
  • || - logical OR. Only one side needs to be true.
  • ! - logical NOT. Flips true to false and false to true.

Combining a few of these together is where conditional logic in Unity starts to feel genuinely powerful:

if (isGrounded && Input.GetButtonDown("Jump"))
{
    Jump();
}

That single line reads almost like a sentence: if the player is grounded AND the jump button was just pressed, then jump. Both conditions have to be true, if either one fails, nothing happens.


The switch Statement: A Cleaner Alternative

Once you've chained four or five else if blocks checking the same variable, your code starts to look messy. A switch statement handles that exact situation more cleanly.

switch (playerState)
{
    case "Idle":
        Debug.Log("Player is idle.");
        break;
    case "Running":
        Debug.Log("Player is running.");
        break;
    case "Jumping":
        Debug.Log("Player is jumping.");
        break;
    default:
        Debug.Log("Unknown state.");
        break;
}

Each case checks the same variable against a different value, and break stops it from falling through into the next case. The default case catches anything that didn't match. It's not a replacement for every if else statement Unity developers write; it really only shines when you're comparing one variable against several possible values, like a state machine or an enum.


So Which One Should You Actually Use?

Here's the no-fluff version, the gut check you can run through in your head:

  • One condition, one outcome → plain if
  • One condition, two possible outcomes → if / else
  • Several conditions checked in sequence → if / else if / else
  • One variable compared against many fixed values → switch
  • Multiple conditions that all need to be true → combine with &&
  • Any one of several conditions is enough → combine with ||

Picking the right shape for your conditional logic in Unity isn't about memorizing syntax, it's about asking "how many outcomes am I actually dealing with, and am I checking one thing or several?" That question points you at the right structure almost every time.


Wrapping Up

Conditional logic doesn't feel like much on its own, just a handful of true/false checks, but it's genuinely the backbone of every interactive system you'll ever build. Health checks, input handling, AI decisions, UI state, it all comes back to some variation of "if this, then that." Get comfortable with if, else, and switch, and you've got the foundation nearly every other Unity system sits on top of. 


 ~happy Coding!

No comments:

Post a Comment