Skip to main content
Unity3D

Introduction to C# in Unity #11: Unity If conditional statement

By February 16, 2021July 17th, 2022No Comments
If Conditional

https://www.youtube.com/watch?v=Fxrt08AkRYo&feature=youtu.be

Unity If statement


We use conditional statements all the time in real life. The Unity if conditional statement is used to conditionally execute logic depending on a condition or set of conditions. For example, suppose you go to your favorite restaurant and you ask for what you always eat there –

Fish & Chips

The server lets you know that you can’t order that since they’ve run out of Fish and to please choose something else. So you order your second best choice – 

Pizza Another example:

If

 you do you’ve done your homework only then can you play game.

Else

you need to finish your homework first.
In your code when you're making games you can control the way your program behaves/executes with conditional statements. If conditional statements can control the output of your program based on whether something is True or False.
Conditional statements are often used with comparison and equality operators to conditionally execute code in your game.
There are 3 conditional statements that you can use:
1. If
2. Else
3. Else if

Use if to specify a block of code to be executed, if a specified condition is True. Keep in mind that the code inside the If conditional statement will only execute if the condition is true. If the conditiona is false, then execution is skipped and there is nothing to do.

Syntax

if(your condition here){

//code to execute ONLY if the condition is TRUE

}

 

Leave a Reply