If Blocks in C# | Unity Tutorial

Suppose we wanted to run code only when a certain condition is met. For instance, a player could only fly in a game if they had a jet pack. A player could only buy a bicycle if they had enough money. To implement this kind of functionality, we can use if blocks. If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!

An if block runs code when a specified condition passes. In the Start method from our previous tutorial, use the following format to create an if block.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if () {

}
}

The code in between the if block’s parentheses will contain the condition that needs to be met. If the condition passes, the code in the scope definition (between the curly braces) will execute.

Give the if block the following condition. The if block will execute code because 1 plus 1 equals 2.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if (1 + 1 == 2) {

}
}

Note that a single equals sign (=) assigns a value. A double equals sign (==) is a comparison.

Type the following code in the if block. The string “We’re here!” will print in the console if the if block’s condition is true.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if (1 + 1 == 2) {

}
}

Save the script, and open Unity. Press Play. The message “We’re here!” will print in the console. This means that the if block’s condition passed.

Let’s test a different condition in the if block. Use the following code to make the if block test if the string “cats” equals the string “dogs”.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if ("cats" == "dogs") {
Debug.Log ("We're here!");
}
}

Save the script, and open Unity. Press Play. A warning will appear in the console stating that there is unreachable code at Line 14 of Cube.cs.

This warning appears because the compiler already knows that “cats” will never equal “dogs”. Thus the compiler automatically converts the condition in the parentheses to false. The code in the if block will never be executed.

What if we want the if block to execute code only if a condition is false? Create the following if block, which will run code when “cats” is not equal to “dogs”. The exclamation mark means “NOT”.

// Use this for initialization
void Start () {
transform.name = ImproveName(newName);

if ("cats" == "dogs") {
Debug.Log ("We're here!");
}

if ("cats" != "dogs") {
Debug.Log ("We're there!")
}
}

Save the script, and open Unity. Press Play. The message “We’re there!” will print in the console.

Want to learn more about if blocks? Check out our Unity3D course, where you build 30 virtual reality games!

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com