Time for action – add "else" to the if statement

if-else statements are just like regular if statements with the else option added.

  1. Modify LearningScript as shown in the next screenshot.
  2. Save the file.
  3. In Unity, click on Play.
    Time for action – add "else" to the if statement

Line 14 shows how else, and its code block is simply added after the if code block.

What just happened?

The analysis of code is as follows:

  • The code on line 8 and its description:
    bool theBearMadeBigPottyInTheWoods = false;

    The variable theBearMadeBigPottyInTheWoods is assigned the value of false.

  • The code on line 10 and its description:
    if( theBearMadeBigPottyInTheWoods)

    Since the condition is false, the code block on lines 11 to ...

Get Learning C# by Developing Games with Unity 3D Beginner's Guide 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.