C# Programming With Unity – Conditional Statements

Table of Contents

C# Programming With Unity – Conditional Statements

Reading Time: 4 minutes
Level: Beginner
Version: 2020.3.1 LTS

Help Others Learn Game Development

Share on facebook
Share on twitter
Share on reddit
Share on linkedin

When you are playing a video game a lot of checking is being done behind the scenes.

For example, if the enemy is dead or not, is the player moving or not, does the player have enough mana to throw a special attack, can the enemy detect the player, is a specific item in the inventory full or not, and so on.

In order to achieve all this, we use conditional statements.

The Anatomy Of A Conditional Statement

The most famous conditional statement is the if statement, which goes like this:

				
					if(CONDITION)
{
    // execute code for the condition
}
				
			
The “condition” inside the parentheses is to denote that what we put inside represents a condition that is either true or false.
 
This is where booleans come into play and I mentioned this when we talked about booleans that they are used in conditional statements.
 
For example we can test if a certain number is greater than the other:
				
					if(2 > 1)
{
    // execute code
}
				
			
The condition in this example is 2 > 1, and if this is true then we will execute the code that is between the {} of the if statement. 
 
Of course in this example 2 is always going to be greater than 1 so this if statement is always going to be true.
 
But if we have something like this:
				
					float a = 3f;
float b = 4f;

if(a > b)
{   
    // execute code
}
				
			
In this scenario we are testing if variable a is greater than b, which in this case is not true.
 
But variable a can be our input variable where we will store if we are pressing a button or not.
 
So when we press the button it will have a greater value if we don’t press a button it will have a lower value, we will see examples of this later down the road. 
 
The point is that we are testing a condition whether it is true or false, and based on that execute the appropriate block of code.

How To Test And Compare Numbers To Each Other

As a side note, since we were testing numbers and more specifically if one number is greater than the other one, we can also perform the following tests when it comes to numbers:

				
					// if number is less than the number
if(number1 < number2)

// if number is greater than or equal to the number
if(number1 >= number2)

// if number is less than or equal to the number
if(number1 <= number2)

// if the number is equal to the number
if(number1 == number2)
				
			

Since we are using “=” sign to assign a value to a variable, in order to test if a number on the left side is equal to the number on the right side we use the double equal sign “==”.

If Else Statement

Okay but what happens if the condition that we are testing is not true, then we can’t execute the code we want to execute, what can we do?
 
Well there is a solution for everything.
 
You can use the else statement and give an alternative in case the condition is not true. For example:
				
					if(playerHealth <= 0)
{
    // kill the player
} 
else
{
    // the game continues
}
				
			

This is a concrete example of something that we will do often in our games which is test if the player’s health is less than 0 to kill the player or continue the game.

Of course we are not limited to only using else statement, because there can me multiple scenarios that can occur for a specific part of your game and you want to act accordingly.

Let’s say you have multiple power boosts that active depending on the current value of the players health, you can do something like:

				
					if(playerHealth < 10)
{
    // give power boost 1
} 
else if(playerHealth < 40)
{
    // give power boost 2
} 
else if(playerHealth < 70)
{
    // give power boost 3
}
else
{
    // give power boost 4
}
				
			

This conditional statement will test player’s health if it’s less than 10 to give power boost 1, if it’s less than 40 it will give power boost 2, power boost 3 if health is less than 70, and power boost 4 for any other value from 70 and up.

The AND & OR Operators

You can also test multiple conditions in one if statement by using the && or || sign. If we type:

				
					if(playerHealth > 10 && playerHealth < 50)
{
    // execute code
}
				
			

We are testing if the player’s health is greater than 10 AND if it’s less than 50, both of these conditions need to be fulfilled in order for this whole statement to be true.

This means that if playerHealth has a value of 45 then the above if statement will be true because 45 is greater than 10 and at the same time it is less than 50. 

But if playerHealth has a value of 50, then this if statement is not true because while 50 is greater than 10 it is not less than 50 because 50 is equal to 50, which means that is not true. The same goes for any other number above 50.

If playerHealth is 51 then the statement is not true since 51 is not less than 50. If the value is 9 then the statement is also not true because 9 is not greater than 10 even though it is less than 50.

If we rewrite this condition into:

				
					if(playerHealth > 10 || playerHealth < 50)
{
    // execute code
}
				
			

In this case this statement will be true if one of the conditions on the sides of || is true.

If the playerHealth has a value of 9 then the condition will be true because even though 9 is less than 10, it is also less than 50.

So the first test if playerHealth > 10 will fail, but if playerHealth < 50 will pass and then the code between the {} will be executed.

From all these examples you probably came to the conclusion that using && is testing if the condition on the left AND the condition on the right are true e.g. both of these conditions need to be true in order for this one if statement to be true.

And when you test with || you are testing if one of the conditions is true e.g. if the condition on the left or the condition on the right is true, then the if statement will be true.

So when you are using || one of the conditions needs to be true for the whole if statement to be true, and if you are using && then all the conditions need to be true for the whole if statement to be true.

Nesting If Statements

You can also nest if statements within if statements. For example:

				
					if(playerHealth < 80)
{
    if(playerHealth < 50)
    {
    
    }
}
				
			
And this can go on and on. But as a general rule don’t overnest if statements and with this I mean don’t have 10 if statements within other if statements.
 
You can test of multiple conditions like in the example above depending on the situation of your game, but you can also use the && and || to do the same as well.

The Switch And Case Statement

We can also use switch and case to test for different conditions in our game:

				
					float health = 100f;

switch(health)
{
    case 100:
        // code to execute
    break;
    
    case 90:
        // code to execute
    break;
    
    case 80:
        // code to execute
    break;
}
				
			

The example above takes the health variable and puts it in a switch, then every case acts like a potential value that the health variable can have.

So if the health variable has a value of 100 then the first case will be true and the code below it will be executed, after that the break statement will be reached which will exit outside of this switch and case testing.

The reason for that is because this acts like if else statement, so if we test:

				
					if(health == 100)
{
    // execute code
} 
else if(health == 90)
{
    // execute code
}
				
			

If the health variable is equal to 100, then all other if else statements will not be executed since we reached the one that is true, similarly in this example.

If the case 100 is true, when it executes its code, the break statement will kick in and other cases will not be even tested.

You can use the default condition to execute your code if all the cases that you test are not true:

				
					case(health)
{
    case 100:
        Debug.Log("Health is 100");
    break;
        
    case 90:
        Debug.Log("Health is 90");
    break;
    
    case 80:
        Debug.Log("Health is 80");
    break;
    
    default:
        Debug.Log("Health is not equal to values above");
    break;
}
				
			

In this example if the health variable is not equal to 100, 90 or 80 then the default case will execute its code.

There will be multiple examples of both if else and switch and case statements and we will use them a lot in our development.

For now, you can experiment on your own and try putting in different variables in if else statements as well as switch and case statements and use Debug.Log to print the cases that are true and that way practice what we talked about in this lecture.

Where To Go From Here

In this lecture you learned how to use conditional statements to control the workflow of your game.

To continue your learning journey you can take a look at our Introduction To Loops Lecture which is the next lecture in this tutorial series.

Leave a Comment