Unity Basics Course: Conditional Statements – else

Unity is a very powerful and flexible engine for game development. A basic understanding of programming languages is essential to understand the systematic flow of game development. In this course, we will take a closer look at one of the fundamental concepts of programming: conditional statements, particularly the ‘else’ statement.

1. Basics of Conditional Statements

Conditional statements are used to control the flow of a program based on certain conditions. The most common form is the ‘if’ statement. The ‘if’ statement defines a block of code that executes when the condition is true. However, if you want to perform different actions when the condition is false, the ‘else’ statement is used. The ‘else’ statement is used alongside the ‘if’ statement to help handle complex conditions.

2. Basic Structure of ‘else’ Statement

The ‘else’ statement has the following structure:

                if (condition) {
                    // Code to execute if the condition is true
                } else {
                    // Code to execute if the condition is false
                }
                

In the example above, the ‘if’ statement evaluates the condition, and based on the result, different code blocks are executed.

3. Example Usage of ‘else’ Statement

Let’s see an example of using the ‘else’ statement in actual Unity code. Below is a simple example that outputs messages based on the player’s score:

                void Update() {
                    int score = 10; // Player's score
                    if (score >= 20) {
                        Debug.Log("Score is 20 or more!");
                    } else {
                        Debug.Log("Score is less than 20.");
                    }
                }
                

In the code above, different messages are printed depending on whether the player’s score is 20 or more or not. By using the ‘else’ statement in this way, you can implement various actions based on conditions.

4. Compound Conditional Statements and ‘else if’

The ‘else’ statement is very useful for handling compound conditions as it is used with the ‘if’ statement. Using the ‘else if’ statement allows you to check additional conditions. Here is an example:

                void Update() {
                    int score = 15;
                    if (score >= 20) {
                        Debug.Log("Score is 20 or more!");
                    } else if (score >= 10) {
                        Debug.Log("Score is 10 or more.");
                    } else {
                        Debug.Log("Score is less than 10.");
                    }
                }
                

This code handles three cases based on the score. Only the block for the true condition will execute, allowing for multiple condition branching using ‘else if’.

5. Use Cases for Conditional Statements

Below is an example of a conditional statement that can be useful in a game. This example assumes a situation where the accessible area is determined based on the player’s character’s state:

                bool hasKey = false; // Whether the player has the key
                void Update() {
                    if (hasKey) {
                        Debug.Log("You can enter here because you have the key.");
                    } else {
                        Debug.Log("You need a key!");
                    }
                }
                

In this way, conditional statements become a powerful tool for controlling various situations within a game.

6. Optimization and Considerations for Conditional Statements

When using a lot of conditional statements, the code can become complex and inefficient. Here are some points to consider when using conditional statements:

  • Avoid nesting multiple conditional statements. Complex nesting decreases readability and makes maintenance difficult.
  • Express conditions clearly. If the conditions are complex, write them so that their meaning is easily understandable.

7. Conclusion

The ‘else’ statement plays an important role in Unity programming and is an essential tool for controlling the flow and actions of the game. Through this course, I hope you understand the basic usage of conditional statements and apply them in actual game development. By effectively utilizing conditional statements, you can create very diverse game logic.