• $10 Game Dev' Courses - Use code "BREAKTHRU"

    Video courses

C# for Beginners: Learn Fundamentals by Coding

Highly acclaimed course: Over 13,000 students

C# Intermediate: Classes, Interfaces, and OOP

In-depth, classes, interfaces, and OOP.

Learn To Code By Making a 2D Platformer in Unity

Beginning Unity & C# with a real game

Condition checking in games is all about making decisions. How do we “know” if the player has completed the level or crashed a ship? How do we detect if the ball has reached the edge of the screen and needs to bounce back the other way? C# has the if keyword which helps us detect conditions and make decisions.

About this tutorial

Skill level 1
Time to read: 10 minutes

New Concepts:

  1. C#  if keyword
  2. C# logical operators
  3. Making decisions in C#

Projects related to or that demonstrate these concepts

Asking questions in C#

We need to be able to use C# to ask questions, perhaps things like this:

If the ball has reached the edge of the screen

     - reverse the direction of the ball.

If the player loses his last life

     - end the game.

If the previous highest score been beaten?

     - update the hiScore variable.

The above three examples are not real code. It is called pseudo code. We will see real C# code using if soon, as well as use if for real in the first Unity project of this course. Also note how the action (e.g. Reverse the direction of the ball etc.) is indented from the edge of the page? We will use indents to make our code more readable and I strongly encourage you to do the same.

Some more pseudo code, but getting nearer to real C# is this:

if(some condition is true) {

     // do whatever

}

The if keyword is followed by the opening and closing parentheses (). Inside the parentheses we have a condition and afterwards, contained in opening and closing curly braces {} we have the code that will run if the condition is true.

At the moment we will concern ourselves with the condition and when we use Unity we will see some examples of the code that executes when the condition is true. For this tutorial we will look at the most useful possibilities we can use with the C# if keyword to create conditions we can then test to see if they are true.

Let’s look at some more operators. This time some comparison operators that we can use to check for conditions in our games.

C# Comparison operators

We can already add +, take away -, multiply *, divide /, assign =, increment ++, and decrement -- with operators. Let’s introduce some more useful operators and see how to actually use them in C#. Don’t worry about memorizing every operator given here. Glance at them and their explanations and then move quickly on to the next tutorial. Practice will be more useful then trying to remember them all.

Note that in all the examples that follow we invent some variables. For any of these examples to work in real code the variables would need to be declared and initialized.

First we have == which is the equality or comparison operator, not to be confused with the assignment operator =. It tests for equality and is either true or false. We can create and test conditions like this following C# code.

if(playerLives == 0){

     // End the game.

}

Next we have ! which is the logical NOT operator. Here it is in action.

if(!playing){

     // Quit to main menu

}

The following, != is another comparison operator, which tests if something is NOT equal. For example:

if(playerAnswer != actualAnswer){

     // Player has got it wrong

}

The hopefully familiar looking >, is another comparison operator, which tests if something is greater than something else. The expression:

if(score > highScore){

     // New high-score

}

Similar but different, >=. This operator tests whether one value is greater than or equal to the other, and if either is true, the result is true. Notice in the next two examples we can use literal values in if statements as well as variables. For example, the expression:

if(10 >= 9){

     // is true.

}

Also look at this for further clarification.

if(10 >= 10){

     // is also true because 10 equals 10
     // even though 10 is not greater than 10

}

<=: Like the preceding operator, this operator tests for two conditions but this time, less than or equal to. The expression,

if(10 <= 9){

      // is false.

}

However, both of these conditions are true.

if (10 <= 10){

     // is true because 10 equals 10

}

if (9 <= 10){

      // is true because 9 less than 10

}

&&: This operator is known as logical AND. It tests two or more separate parts of an expression and ALL parts must be true in order for the result to be true. Logical AND is usually used in conjunction with the other operators to build more complex tests. The expression:

if((10 > 9) && (10 < 11)){

     // is true because both parts are true.

}

However, this next C# code evaluates to false:

if((10 > 9) && (10 < 9)){

     // is false because only one part of the expression
     // is true (10 > 9) and the other is false (10 < 9).

}

This operator || is called logical OR. It is just like logical AND except that only one of two or more parts of an expression need to be true for the expression to be true. Let’s look at the last example we used but replace the && sign with ||. The expression,

if((10 > 9) || (10 < 9)){

     // is now true because...
     // one part of the expression is true (10 > 9)

}

Here are a few examples. You don’t need to fully understand how the code in the next few samples works just take a look at it and think about how we are controlling our game code with the if statements and their conditions.

bool hitDetected = false;

// Some C# that detects
// if the player ship was hit goes here

if(hitDetected) {

     playerLives --;

}

Now look at how we might check to see if the player had got a new fastest time at the end of a racing game. As with the other examples we assume the variables would have been declared and initialized previously.

//check for new fastest time

if(timeTaken < fastestTime) {

     // the player took less time than the fastest time!!
     // So a new fastest time has been set

     // Now change the fastest time variable
     // to the new fastest time (timeTaken)
     fastestTime = timeTaken;

}

We have spent allot of time detecting conditions and making decisions but apart from that very last example we haven’t actually done anything when a condition has been detected. Note that almost any code at all can go inside the curly braces {} of an if statement (including more if statements).

At last it is time to put in to practice all the C# we have learnt so far. If you haven’t already you need to install Unity and then you can complete C# & unity part 1.