A Game Developer's Guide to Booleans in C#
- Beginner C#
- Set Up & Getting Started
November 27, 2025

Booleans in C# are like on/off switches. They're often used with conditional statements to control the flow of your code's execution.
Booleans can only be one of two values: true or false. These values have the same meaning in programming as they do in English, and they play a part in how booleans are named.
This article assumes you know how to use variables, operators, numbers, and strings, and will reference them in code examples.
If you're a visual learner, scroll to the bottom for the video version, or you can watch it on YouTube.
How to Use Booleans in Unity
Boolean variables or constants are created using the bool keyword. The variable is assigned a value of either true or false.
If you don't assign a Boolean variable a value, it automatically defaults to false.
Tip
true and false are special keywords in C#. You can't use them to name anything without some modification.
It's a common convention to name Booleans by prefixing "is" or "has" to the name, such that the name sounds like it's asking a question.
bool isGrounded = true;
var hasPowerUp = false;In addition to making it easier to distinguish Boolean variables from other variables in C#, this naming convention improves your code's readability.
Consider this example. Let's say the game you're building in the Unity Game Engine has a character that can punch or fire their gun, depending on whether they're armed or not at the moment.
You can use a Boolean value, like isArmed, to check if the player is holding a weapon. If this value is true, the character fires their gun. If not, they punch.
var isArmed = true;
if (isArmed)
Debug.Log("Fire the gun."); // <- This line will execute.
else
Debug.Log("Punch.");By using the naming convention, you can read this as, "If the player is armed, fire the gun. Otherwise, punch."
The Boolean value in this scenario acts as a switch. When you turn on a car, the engine runs. Similarly, if the value is true, the switch is on, and the code block gets executed.
When you turn off the car, the engine stops. If the value is false, the switch is off, and the code block isn't executed.
if statements are covered in more detail in the conditional statements article, but they are strongly tied to Booleans, so they'll be mentioned briefly.
Tip
You can use Booleans in Unity as a literal switch to turn the lights in a room on or off.
The Relationship Between Booleans and Operators
Comparison operators and equality operators result in a Boolean value. In a way, these operators ask a question. They can also be used in the condition of a conditional statement.
Let's say you want to show a warning message if the player tries to collect crafting material when they've reached the maximum.
You can use a comparison operator to compare the current crafting materials in the inventory, plus the collected ones, with a predefined maximum limit.
If the new total number of crafting items is greater than the limit, you'll show the player a message informing them they can't hold any more crafting items.
const int MaxCraftingMaterials = 5000;
var inventoryMaterials = 4620;
var materialsCollected = 500;
var totalMaterials = inventoryMaterials + materialsCollected;
if (totalMaterials > MaxCraftingMaterials)
Debug.Log("You can't carry any more materials!");Similarly, suppose your Unity game features collectibles, and you want to award the player 500 XP after collecting 10 items,
In that case, you can use an equality operator to check if the player has reached this milestone, then award the XP.
Tip
Equality operators use two "=" signs.
var collectibles = 9;
if (collectibles == 10)
{
// This code block won't be executed because
// 'collectibles' (9) is NOT equal to 10 above.
Debug.Log("You've received 500 XP!");
}The NOT operator, written using a single exclamation mark ( ! ), reverses a Boolean value.
For example, !true is false. You can read it as "NOT true", meaning false. Similarly, !false (NOT false) is true.
The NOT operator is useful for Boolean values that change often. This is evident in scenarios, such as the one mentioned earlier in this article, where the player can either be armed or disarmed.
There'll be times you want to do other things when the player is unarmed, such as carrying heavy objects. For simplicity's sake, assume there's no way to holster the weapon.
Picking up or dropping weapons changes the isArmed Boolean variable, so you can check if the player is unarmed by placing an exclamation mark before isArmed, turning it into "is NOT armed", then lifting an object.
var isArmed = true;
if (!isArmed)
Debug.Log("You can now pick up objects.");Booleans can also be used in classes when declaring fields or properties. Those are explained in more detail in separate articles.
Recap
- Booleans are like on/off switches controlling the execution of different sections of code.
- Boolean names are commonly prefixed with "is" or "has".
- Boolean variables default to
falseif not assigned a value. - Comparison and equality operators result in a Boolean value.
- The NOT operator reverses a Boolean value.
This article is the sixth in a series breaking down common C# concepts used in game development with the Unity Game Engine.
The next one discusses how to use enums to work with multiple related constants.
Here's the complete list of articles in the series:
- A Game Developer's Guide to Variables in C#
- A Game Developer's Guide to Constants in C#
- A Game Developer's Guide to Numbers in C#
- A Game Developer's Guide to Operators in C#
- A Game Developer's Guide to Strings in C#
- A Game Developer's Guide to Booleans in C# (you are here)
- A Game Developer's Guide to Enums in C#
- A Game Developer's Guide to Conditional Statements in C#
- A Game Developer's Guide to Loops in C#
- A Game Developer's Guide to Classes in C#
- A Game Developer's Guide to Access Modifiers in C#
- A Game Developer's Guide to Fields & Properties in C#
- A Game Developer's Guide to Methods in C#
- A Game Developer's Guide to Lists & Arrays in C#
- A Game Developer's Guide to Dates & Times in C#


