Unity Basic Course: Conditional Statements – if

Unity is a very important engine in game development, and coding is also essential for implementing game logic. In this tutorial, we will explore how to use the if statement, a conditional statement in Unity using C#, along with various examples of its applications.

Basic Concept of Conditional Statements

A conditional statement is a structure that controls the flow of a program, executing different commands based on whether a given condition is true or false. In C#, conditional statements are primarily implemented using the if statement.

Basic Syntax of the if Statement

if (condition) {
        // Code to execute when condition is true
    }

In the above syntax, the ‘condition’ part is a Boolean expression that should return true or false. The code inside the curly braces is executed only when the condition is true.

Example of Using the if Statement

Below is a basic example of using the if statement in a Unity script.

Example 1: Checking Player’s Health

using UnityEngine;

    public class Player : MonoBehaviour {
        public int health = 100;

        void Update() {
            if (health <= 0) {
                Debug.Log("The player has died.");
            }
        }
    }

In this example, the Update method is called every frame. If the player’s health drops to 0 or below, a message saying “The player has died.” will be logged.

Using the else Statement

By using the else statement in conjunction with the if statement, you can specify the code that will be executed if the condition is false.

using UnityEngine;

    public class Player : MonoBehaviour {
        public int health = 100;

        void Update() {
            if (health <= 0) {
                Debug.Log("The player has died.");
            } else {
                Debug.Log("The player is healthy.");
            }
        }
    }

Nested if Statements

Conditional statements can be nested, allowing for more complex logic. The following example checks both the player’s health and their weapon status.

using UnityEngine;

    public class Player : MonoBehaviour {
        public int health = 100;
        public bool hasWeapon = false;

        void Update() {
            if (health <= 0) {
                Debug.Log("The player has died.");
            } else {
                if (hasWeapon) {
                    Debug.Log("The player is equipped with a weapon.");
                } else {
                    Debug.Log("The player is not equipped with a weapon.");
                }
            }
        }
    }

Using else if Statements

You can use else if to handle multiple conditions, allowing you to process various cases simultaneously.

using UnityEngine;

    public class Player : MonoBehaviour {
        public int health = 100;

        void Update() {
            if (health > 75) {
                Debug.Log("The player is healthy.");
            } else if (health > 50) {
                Debug.Log("The player is tired.");
            } else if (health > 25) {
                Debug.Log("The player is in danger.");
            } else {
                Debug.Log("The player has died.");
            }
        }
    }

Using Logical Operators in Conditional Statements

Conditional statements can use logical operators such as AND (&&) and OR (||). You can combine multiple conditions to create more complex conditions.

Using the AND Operator

using UnityEngine;

    public class Player : MonoBehaviour {
        public int health = 100;
        public bool hasWeapon = true;

        void Update() {
            if (health > 0 && hasWeapon) {
                Debug.Log("The player is alive and has a weapon.");
            } else {
                Debug.Log("The player is in a dangerous state.");
            }
        }
    }

Using the OR Operator

using UnityEngine;

    public class Player : MonoBehaviour {
        public int health = 100;
        public bool hasWeapon = false;

        void Update() {
            if (health <= 0 || !hasWeapon) {
                Debug.Log("The player has died or does not have a weapon.");
            } else {
                Debug.Log("The player is alive and has a weapon.");
            }
        }
    }

Conclusion

In this tutorial, we have thoroughly explored how to use the if statement in Unity. Conditional statements are very important in controlling the game’s logic, and to create complex games, one must be able to utilize various conditional statements. Make sure to implement if, else, else if, and logical operators appropriately to create your own game logic. Solidify your foundation in Unity and strive to grow into a more advanced game developer.