Conditional statements

Variables change in potentially many different circumstances: when the player changes their position, when enemies are destroyed, when the level changes, and so on. Consequently, you'll frequently need to check the value of a variable to branch the execution of your scripts that perform different sets of actions, depending on the value. For example, if PlayerHealth reaches 0 percent, you'll perform a death sequence, but if PlayerHealth is at 20 percent, you might only display a warning message. In this specific example, the PlayerHealth variable drives the script in a specified direction. C# offers two main conditional statements to achieve a program branching like this. These are the if statement and the Switch statement. ...

Get Mastering Unity Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.